Upon trying to get my response in JSON
using Spring 3.x
, I get the 406 error
\"The resource identified by this request is only capable
There is nothing wrong in your configuration, let me suggest a few small changes though:
a) Your namespaces appear wrong - they are referring to the 3.0 schemas, just change them to either 3.1 one's or don't refer to the version explicitly, this way for eg.
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
OR
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
b) You don't require the ContentNegotiatingViewResolver, you can remove everything but the component-scan
and <mvc:annotation-driven/>
from your configuration
c) The request will not directly work from the browser as it explicitly requires an Accept header of "application/json" - $.getJson
call should work though as it sends the correct headers
d) Remove the headers=Acc..
from the @RequestMapping, and produces also, both are filtering criteria to match up the correct mapped method call.
With these, there is no reason why the json should not get served out, can you please try with these and see how it goes.
Shortly:
For Spring MVC 4.1.6 there is enough:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.0</version>
</dependency>
jackson-databind has dependency on core and annotations artifacts.
In details:
What is HTTP 406 error?
406 Not Acceptable The requested resource is only capable of generating content not acceptable according to the Accept headers sent in the request.
It means that Server cannot generate content which MEDIA TYPE stated in Accept Header.
But how does server know which MEDIA TYPE it can generate and which not?
Spring Web has concept of HttpMessageConverter. Some of these converters are already registered in Spring and AbstractMessageConverterMethodArgumentResolver holds them in property messageConverters.
During request processing AbstractMessageConverterMethodProcessor analyzes what spring can convert and saves all supported MEDIA TYPES in producibleMediaTypes variable. If requested MEDIA TYPE is not producible then says Error 406 == I cannot generated requested media type. Sorry.
To cut the long story short - register required converters. In your case it's jackson library which produces application/json
If you are using Spring 4 then you must only update your libraries:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.0</version>
</dependency>
I think you need to add a produces="application/json" to your @RequestMapping (haven't looked at spring mvc in a while so i'm not 100% positive) ...
http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html
16.3.2.6 Producible Media Types
You can narrow the primary mapping by specifying a list of producible media types. The request will be matched only if the Accept request header matches one of these values. Furthermore, use of the produces condition ensures the actual content type used to generate the response respects the media types specified in the produces condition. For example:
@Controller
@RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, produces="application/json")
@ResponseBody
public Pet getPet(@PathVariable String petId, Model model) {
// implementation omitted
}
Please, see http://docs.spring.io/spring-framework/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-config-content-negotiation As you can see
"For file extensions in the request URI, the MVC Java config and the MVC namespace, automatically register extensions such as .json, .xml, .rss, and .atom if the corresponding dependencies such as Jackson, JAXB2, or Rome are present on the classpath."
You should add ".json" at the end of URI (like http://domain/SpringWebProject/json/contest/abcd.json) It works for me.
Maybe you should update your jackson library. I use Spring v4.3.8 and I use it as follows:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.7</version>
</dependency>