Spring 4.1.1 RELEASE和@ResponseBody返回HTTP 406

Din*_* Tw 11 json spring-mvc http-status-code-406

我正在使用@ResponseBody返回Spring MVC中的Json对象.它在版本4.0.7和3.2.11上按预期工作,但当我尝试使用最新的Spring版本4.1.1(截至10/16)而没有任何其他配置更改时,它返回HTTP状态406.这被认为是一个错误还是4.1.1需要不同的配置?

最新的jackson jar已经在classpath中了

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

Spring 文档上的示例运行正常

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

当返回类型是String时.当返回类型是POJO时会发生问题.

Vit*_*ito 17

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>
Run Code Online (Sandbox Code Playgroud)

和spring mvc配置文件(例如: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>
Run Code Online (Sandbox Code Playgroud)


Ali*_*ose 8

删除杰克逊1.*取代2.4.4(JAXRS),它将导入所有其他依赖项,jackson-core,jackson-databind和jackson-annotations.

删除

<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>
Run Code Online (Sandbox Code Playgroud)

添加

<dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-base</artifactId>
    <version>2.4.4</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

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

并在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>
Run Code Online (Sandbox Code Playgroud)

如果在类文件中导入了jackson annotate,则也应该替换它

删除

import org.codehaus.jackson.annotate.JsonIgnoreProperties

添加

import com.fasterxml.jackson.annotation.JsonIgnoreProperties