解决@ResponseBody 406

#解决@ResponseBody 406

使用SpringMvc的 @ResponseBody,想返回Json,出现406

经google得知需要如下配置。

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="mappingJackson2HttpMessageConverter" /><!-- json转换器 -->
        </list>
    </property>
</bean>

not work,继续报错。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0' defined in ServletContext resource [/WEB-INF/springmvc-servlet.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper

很明显了,少了jackson的依赖。

在pom里添加

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

done!