springmvc @ResponseBody返回json 报406 not acceptable

来源:互联网 发布:centos 修改ssh端口 编辑:程序博客网 时间:2024/04/28 07:15

今天在整合小项目时,碰到一个问题406  , controller中使用springMVC的@ResponseBody来返回json格式数据,出现了406,controller代码如下

@Controller@RequestMapping(value = "/user/*")public class UserController {@Autowiredpublic UserService userService;@RequestMapping(value = "user.do")@ResponseBodypublic User user(String userId) {User user = userService.getUserById(userId);return user;}}

spring.xml文件如下

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"><!-- 引入dbconfig.properties属性文件 --><context:property-placeholder location="classpath*:conf/*.properties" /><!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射 --><!-- 通过注解,把URL映射到Controller上,该标签默认注册RequestMappingHandlerMapping和RequestMappingHandlerAdapter --><mvc:annotation-driven /><!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean --><context:component-scan base-package="com.hn.alter.*" /><!-- 加载组装所以配置文件 context:component-scan注册后可以省略当前配置 <context:annotation-config /> --><import resource="spring-mybatis.xml" /><!-- 防止配置url-pattern时过滤静态文件 --><mvc:resources location="/" mapping="/*" /><mvc:resources mapping="/resources/**" location="/resources/" /><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/" /><property name="suffix" value=".jsp" /></bean></beans>
pom.xml文件如下

<!-- json --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.7.1</version></dependency><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><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>2.7.1</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.7.1-1</version></dependency>
其中网友也出现了一些其他错误导致的,有以下几种:

1、没有配置<mvc:annotation-driven />

<strong><!-- 通过注解,把URL映射到Controller上,该标签默认注册RequestMappingHandlerMapping和RequestMappingHandlerAdapter --><mvc:annotation-driven /></strong>
2、没有添加jar包,但是我添加了这两个包

jackson-core-asl.jar和jackson-mapper-asl.jar
3、没有配置注解handler(跟spring版本有关,我用版本不用配置这个,自动注入了RequestMappingHandlerMapping和RequestMappingHandlerAdapter见情况一)

<strong>    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">              <property name = "messageConverters">                <list>                 <bean class = "org.springframework.http.converter.StringHttpMessageConverter">                      <property name = "supportedMediaTypes"><list><value>text/plain;charset=UTF-8</value></list></property>                 </bean>                </list>               </property>          </bean>  </strong>
4、也就是我碰到的问题,没有添加另外一个包jackson-databind.jar,苦笑不得

<strong><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.7.1-1</version></dependency></strong>
记录一下希望也碰到这个问题的朋友及时解决

0 1
原创粉丝点击