Spring 4.1.1 RELEASE和@ResponseBody返回HTTP 406


问题内容

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

最新的杰克逊jar已经在类路径中

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

Spring 文档上的示例运行正常

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

当返回类型为String时。当返回类型是POJO时,会发生问题。


问题答案:

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>

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