This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Spring and the fly likes Spring MVC/REST controller: how to convert java to JSON Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring MVC/REST controller: how to convert java to JSON" Watch "Spring MVC/REST controller: how to convert java to JSON" New topic
Author

Spring MVC/REST controller: how to convert java to JSON

Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 381
The following controller is working because I can single step thru it:

I use this command line:


However, I get this error from curl:


OK, so what do I have to do to fix this? Somewhere it needs to know how to generate JSON and it is not doing it.
Can someone guide me?
Thanks
Siegfried
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

Siegfried

Have you tried adding produces = "application/json" to your RequestMapping annotation?
Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 381
OK, I tried your suggestion:


I'm still getting

"The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers."
Thanks
Siegfried
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 415
    
    3

Have a check on this line and its implication.
To me, the server is saying I'm expecting a header tag Content-Type=text/xml or application/json.

Did the client send a header tag

Content-type: text/xml or application/json

James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

Siegfried, what version of Spring are you using?
udaykiranreddy desam
Greenhorn

Joined: Jun 12, 2012
Posts: 10
Hai just configure ContentNegotiatingViewResolver in your context and add .json to your request

cool


or just add @ResponeBody in your controller
 
jQuery in Action, 2nd edition
 
subject: Spring MVC/REST controller: how to convert java to JSON