Solved

help with converting spring mvc ws with resteasy ws?

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java SE 8u111  Lot of stuff broke 11 69
javap bin 2 34
runtime exception 2 27
Facing this issue for maven proxy setting 2 9
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…

785 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