Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

help with converting spring mvc ws with resteasy ws?

Posted on 2014-03-11
5
Medium Priority
?
340 Views
Last Modified: 2014-03-14
I need to convert the spring mvc ws to resteasy.

How can handle the adding one line attribute and pass it along with the result object?


spring mvc ws:

@Controller
@RequestMapping("/customerservice")
public class ServiceTicket {
...
@RequestMapping(value = "servicetypes", method = RequestMethod.GET)
	public void allServiceTypes(HttpServletResponse response) {
List<ServiceType> res = serviceTicketDao.getAllServiceTypes();

writeJsonResponse(response, res);
}

private void writeJsonResponse(HttpServletResponse response, List<?> obj) {
		response.setContentType("application/json");
		try {
			PrintWriter out = response.getWriter();
			out.print("{\"success\":true,\"total\":" + obj.size() + ",\"root\":");			
                 out.print(jacksonObjectMapper.writeValueAsString(obj));
			out.print("}");
		}
		catch (IOException e) {
		}
	}

Open in new window


resteasy ws:
@Path("/customerservice")
public class ServiceTicketWs {

@Path("/servicetypes")
	@GET()
	@Produces("application/json")	
	public void allServiceTypes() {

List<ServiceType> res = serviceCatalogDao.getAllServiceTypes();

[b]//Q: resteasy will handle coverting to json now, but how can I add this line to the 
//\"success\":true,\"total\":" + obj.size() + ",\"root\":")?	
//writeJsonResponse(response, res);
[/b]}

private void writeJsonResponse(HttpServletResponse response, List<?> obj) {
		response.setContentType("application/json");
		try {
			PrintWriter out = response.getWriter();
			out.print("{\"success\":true,\"total\":" + obj.size() + ",\"root\":");			
                 out.print(jacksonObjectMapper.writeValueAsString(obj));
			out.print("}");
		}
		catch (IOException e) {
		}
	}

Open in new window

0
Comment
Question by:dkim18
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 

Author Comment

by:dkim18
ID: 39921838
What if I have this json string, is it ok to return it as it is?

String jsonString="{\"success\":true,\"total\":10}";
@Path("/getoneservice")
	@GET()
	@Produces("application/json")	
	public Response getOneSerivceExample() {

String jsonString="{\"success\":true,\"total\":10}";

ResponseBuilder builder = Response.ok(jsonString);		

		//builder.entity(jsonString);
				return builder.status(200).build();

 
}

Open in new window


Can I do this?

I didn't get to test it yet.
0
 
LVL 36

Accepted Solution

by:
mccarl earned 2000 total points
ID: 39922317
resteasy will handle coverting to json now, but how can I add this line
I don't know too much about Resteasy (but I do know Spring WS) and if resteasy can automatically handle the conversion to JSON, then you should be able to do something like this...
    @Path("/servicetypes")
    @GET()
    @Produces("application/json")	
    public Map<String, Object> allServiceTypes() {

        List<ServiceType> res = serviceCatalogDao.getAllServiceTypes();

        Map<String, Object> result = new HashMap<String, Object>();
        result.put("success", "true");
        result.put("total", res.size());
        result.put("root", res);

        return result;
    }

Open in new window

Note, as I said above Spring WS can do this automatic conversion from Objects (both POJO's and Collection types like above) to JSON for you too, if that is the sole reason for moving from Spring to Resteasy?
0
 

Author Comment

by:dkim18
ID: 39923366
Thanks mccarl.

No, my client just wants to replace this (the app works fine) with using resteasy because they want the cleaner looks so other team members can understand them better and their projects are all in resteasy.

I will try the Map().
Thanks.
0
 

Author Closing Comment

by:dkim18
ID: 39929033
perfect.
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39930943
Glad to help! :)
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question