Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Configure spring controller to return json response

 
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.
 
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
 
ranger
Posts: 17346
11
Mac IntelliJ IDE 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
Posts: 17346
11
Mac IntelliJ IDE 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
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic