Spring 4.1.1 RELEASE and @ResponseBody return HTTP 406

荒凉一梦 提交于 2019-12-17 19:38:12

问题


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

the latest jackson jar is already in the classpath

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

The example on the Spring document works fine

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

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


回答1:


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 config file (eg: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>



回答2:


Got it working after removing Jackson 1.* replacing with the 2.4.4 (JAXRS), which will import all the 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 the servlet xml

<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 the jackson annotate is imported in the class files, it should also be replaced

Removed

import org.codehaus.jackson.annotate.JsonIgnoreProperties

Added

import com.fasterxml.jackson.annotation.JsonIgnoreProperties




回答3:


I was struggling with similar issue migrating from 3.2 to spring 4.2. Was getting

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type:

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

Combination of both previous answers work as well, if you do not want register custom contentNegotiationManager you can do following:

remove all jackson dependencies from

pom.xml

and use latest one

<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>


来源:https://stackoverflow.com/questions/26416248/spring-4-1-1-release-and-responsebody-return-http-406

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!