Solved

help with converting spring mvc ws with resteasy ws?

Posted on 2014-03-11
5
312 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
  • 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 35

Accepted Solution

by:
mccarl earned 500 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 35

Expert Comment

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

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 64
Error in @AspectJ Based AOP with Spring 2 18
hibernate example for saving data 19 55
going to wrong jsp page 2 33
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

829 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