Spring 4.1.1 RELEASE and @ResponseBody return HTTP 406

I am using @ResponseBody to return a Json object in Spring MVC. It works as expected in versions 4.0.7 and 3.2.11, but it returns HTTP status 406 when I try to use the latest version of Spring release 4.1.1 (as of 10/16) without any other configuration changes. Is this considered a bug or does 4.1.1 require a different configuration?

Jackson's last drum is already on its way to classes

<dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> 

Spring document example works fine

 @RequestMapping(value = "/something", method = RequestMethod.PUT) @ResponseBody public String helloWorld() { return "Hello World"; } 

when the return type is String. The problem occurs when the return type is a POJO.

+11
json spring-mvc
Oct 17 '14 at 0:53
source share
3 answers

Maven pom.xml:

  <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.4.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.4.3</version> </dependency> 

and spring mvc configuration file (ex: spring -mvc.xml)

 <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"/> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> </mvc:message-converters> </mvc:annotation-driven> 
+18
Oct 17 '14 at 3:39
source

I got the job after removing Jackson 1. *, replacing it with 2.4.4 (JAXRS), which will import all other dependencies, jackson-core, jackson-databind and jackson-annotations.

Removed

 <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> 

Added

 <dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactId>jackson-jaxrs-base</artifactId> <version>2.4.4</version> </dependency> 

http://www.codingpedia.org/ama/jquery-ui-autocomplete-not-working-in-spring-4-1/

and in xml servlet

 <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" /> <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <property name="favorPathExtension" value="false" /> <property name="favorParameter" value="true" /> <property name="mediaTypes" > <value> json=application/json xml=application/xml </value> </property> </bean> 

if jackson annotation is imported into class files, it should also be replaced

Removed

import org.codehaus.jackson.annotate.JsonIgnoreProperties

Added

import com.fasterxml.jackson.annotation.JsonIgnoreProperties

+8
Dec 12 '14 at 14:28
source

I struggled with a similar problem, switching from 3.2 to spring 4.2. It turned out

org.springframework.web.util.NestedServletException: request processing failed; inested exception is java.lang.IllegalArgumentException: converter not found for return value of type:

posting it here so people can find it by the name of the exception :) It took me half a day to find this article. Thanks @Vito and @Aias

The combination of both of the previous answers also works, if you do not want to register a custom contentNegotiationManager , you can do the following:

remove all Jackson dependencies on

pom.xml

and use the latest version

 <dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactId>jackson-jaxrs-base</artifactId> <version>2.6.1</version> </dependency> 

servlet.xml

 <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"/> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> </mvc:message-converters> </mvc:annotation-driven> 
+1
Aug 14 '15 at 19:31
source



All Articles