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

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

 
pawan chopra
Ranch Hand
Posts: 417
jQuery Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
H Paul
Ranch Hand
Posts: 471
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 417
jQuery Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply . Value in the header is applicarion/json.
 
H Paul
Ranch Hand
Posts: 471
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the server side,
Have you look at the Producible Media Types. See documentation.
produces attribute of @RequestMapping.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
+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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic