Solved

help with converting spring mvc ws with resteasy ws?

Posted on 2014-03-11
5
324 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 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 36

Expert Comment

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

624 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