Solved

help with converting spring mvc ws with resteasy ws?

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now