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 Error 406: Spring MVC not able to return JSONArray object. 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 "Error 406: Spring MVC not able to return JSONArray object." Watch "Error 406: Spring MVC not able to return JSONArray object." New topic
Author

Error 406: Spring MVC not able to return JSONArray object.

pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 410

Hi All,

I am trying to return JSONArray object from my spring mvc controller. But I am getting 406 error on browser. This is an ajax request and I am using jquery to make the request.

Spring controller method:



I have tried both these entries in spring-context.xml



and




My pom.xml has these dependencies:



I am running WAS 8. Not sure what is the problem. I am able to return simple object in json format but not JSONArray object. Please let me know in case I am doing anything wrong.


Pawan Chopra
SCJP - DuMmIeS mInD
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 412
    
    3
First, check the value of the Accept http header tag that the client (ajax) send to the server.

Use TCP/IP mon to see the http request sent to the server.
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 410

Thanks for the reply . Value in the header is applicarion/json.
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 412
    
    3
On the server side,
Have you look at the Producible Media Types. See documentation.
produces attribute of @RequestMapping.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Personally, I use just

<mvc:annotation-driven/>



Then in my Controller i just return my domain object or List of my domain object with @ResponseBody and it works. No need to add anything else.

In your code, I notice you aren't returning a domain object, but a JSON specific type object. Maybe Jackson doesn't know how to convert that.

What Exception do you get? Can you post the stack trace here?

What does your jquery code look like to make the call?

Ah you say simple objects work. Maybe there is a circular dependency in the JSONArray object. You might need to add some @JsonIgnore annotations in that class. But if you don;t have that code, then you might have to make an xml jackson config for that class.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

+1 to what Mark said. I just wanted to add that jackson 2 support is now in Spring 3.2 and this adds much needed love for handling circular dependencies.


[How To Ask Questions][Read before you PM me]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Error 406: Spring MVC not able to return JSONArray object.