@ResponseBody返回JSON数据时遇到406错误

来源:互联网 发布:ubuntu mono 编辑:程序博客网 时间:2024/06/05 19:32

使用的spring是4.3.0版的,解决方法其实很简单:就是在spring-mvc的配置中少了一句话:<mvc:annotation-driven />

加上这句后就OK了。


下面记录的是问题出现和解决的过程:

==============================================================================

结合EasyUI时将JSON数据显示在datagrid中时出现这个错误。

用Firebug查看时问题如下:


显示出现的错误是:406 Not Accepatable ,其中有一句提示:

The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().

因为easyui的datagrid要求返回的数据是JSON格式的,这句提示说明返回的数据不是JSON格式的。

开始还以为是mybatis中返回的数据有问题,但Controller中直接改为下面的测试方法来返回数据后仍然出现同样的错误。

@Controllerpublic class BookController {@RequestMapping("bookList.do")public @ResponseBody ArrayList<Book> getList(){ArrayList list=new ArrayList();list.add(new Book(10,"aaa","wa",21.8f,100));list.add(new Book(11,"bbb","wa",29.0f,200));list.add(new Book(12,"ccc","www",16.8f,300));System.out.println(list);return list;}}
之前在另一个只有springmvc+easyui没使用mybatis的项目中进行测试是没有问题的,这说明与mybatis无关了,还是spring mvc的问题。查找了很久的原因,网上找到一个解决方案:

<!-- 解决@ResponseBody注解直接返回对象并转换成JSON时出现406问题,同时解决了返回String类型乱码的问题 -->    <mvc:annotation-driven>        <mvc:message-converters>            <bean class="org.springframework.http.converter.StringHttpMessageConverter">                <property name="supportedMediaTypes">                    <list>                        <value>text/plain;charset=UTF-8</value>                        <value>text/html;charset=UTF-8</value>                    </list>                </property>            </bean>            <bean                class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">                <property name="supportedMediaTypes">                    <list>                        <value>application/json; charset=UTF-8</value>                        <value>application/x-www-form-urlencoded; charset=UTF-8</value>                    </list>                </property>            </bean>        </mvc:message-converters>    </mvc:annotation-driven>

这时才发现原来的测试项目的spring-mvc.xml中有一句这样的配置,而在新的测试项目中少了这一句:

<mvc:annotation-driven />

加上这一句后,新的项目也测试通过了。并且利用mybatis从数据库中取得的中文数据也可以正常显示,并不需要前面网上那种解决方案的那一大段配置,网上这种估计适合更低一些版本的。而本人这几个项目中用的spring是4.3.0 RELEASE版的,高版本的spring据说是不用配置MessageConverter的。

而 <mvc:annotation-driven /> 这句配置如果没有,个人猜测应该是连@ResponseBody 这样的注解都不能正常起作用的,因此也可以理解为什么返回到客户端的不是JSON格式的数据了。

测试结果:



这里使用的jackson包分别是:

<!-- json --><!-- <dependency><groupId>org.codehaus.jackson</groupId><artifactId>jackson-mapper-asl</artifactId><version>1.9.13</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.3</version></dependency> --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>${jackson.version}</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>${jackson.version}</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>${jackson.version}</version></dependency>

前面的两个已经注释了没有使用,只使用了jackson-annotation、jackson-core和jackson-databind这3个。


0 0
原创粉丝点击