This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Spring and the fly likes Configure spring controller to return json response Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Configure spring controller to return json response" Watch "Configure spring controller to return json response" New topic
Author

Configure spring controller to return json response

Mark Reyes
Ranch Hand

Joined: Jul 09, 2007
Posts: 426
Hi,

Does anybody know a good library that I could use to return a JSON response from my spring controller? My Webapps is built on Spring MVC 2.5.

I am finding it hard to decide which library is ok as I browse the net. If possible can you send me link on how to do this?

Thanks.


Sean Clark ---> I love this place!!!
Me ------> I definitely love this place!!!
Hongli Li
Ranch Hand

Joined: Oct 29, 2006
Posts: 124
mark reyes wrote:Hi,

Does anybody know a good library that I could use to return a JSON response from my spring controller? My Webapps is built on Spring MVC 2.5.

I am finding it hard to decide which library is ok as I browse the net. If possible can you send me link on how to do this?

Thanks.

have you tried jettison, Jettison library is for for JSON serialization and deserialization


Do you know why this cup is useful? Because it is empty.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Jackson is another library out there.

If you are using Spring 3.0, I highly recommend using Spring's RESTful Web Services instead, so you don't need to do anything in the Controller, it stays the same, and now the client requests what representation they want instead. It supports json, xml, html, rss, atom to name a few. But in all cases the controller code is the same, as it wouldn't be the Controller's responsibility.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Hongli Li
Ranch Hand

Joined: Oct 29, 2006
Posts: 124
Mark's right, if you are using restful webservice it's just a matter of annotating methods in your Resource with
@Produces({MediaType.APPLICATION_JSON})
@Path("/somepath")
public Response/Bean someAction()
Mark Reyes
Ranch Hand

Joined: Jul 09, 2007
Posts: 426
Hi Mark/Hongli,

Thanks for your interest regarding my post.

have you tried jettison, Jettison library is for for JSON serialization and deserialization


I havent heard about jettison, i tried to look up a bit on how to configure this in my spring controller but cant find enough resource on how to do this

If you are using Spring 3.0, I highly recommend using Spring's RESTful Web Services


Unfortunately, the web app is built on Spring 2.5 module and Web services is not an option as of the moment.

While googling at the net, I found this nice library Spring-JSON that has nice tutorial on how to integrate it with
Spring MVC. I am currently unsure though if this is good. Have you heard about this or has anyone used this?

Thanks.
Hongli Li
Ranch Hand

Joined: Oct 29, 2006
Posts: 124
I haven't handcrafted any JSON format for a long time, but I think the basic idea should be something like this, you have your POJOS and you need to convert them to JSON object of your selected choice of JSON lib, then output them as JSON type of response(set the proper type(MIME) of header in http I think it should be "application/json"), GSON is another choice. and the user guide is http://sites.google.com/site/gson/gson-user-guide
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Using a JSON libarary, you can do things like have your Controller method return the library's View object. Like JsonView

Looking at code I see

package net.sf.json.spring.web.servlet.view;

So whichever library has that package structure you can use.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Configure spring controller to return json response
 
Similar Threads
Spring MVC and JSON array
Send/receive request from jsp to a Spring portlet.
JSON response to populate listbox
Undefined on jQuery Ajax Request
JSON in JSP