Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Configure spring controller to return json response

 
Mark Reyes
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hongli Li
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Hongli Li
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 426
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic