您的位置:首页 > 编程语言 > Java开发

Spring MVC json报406错误的解决办法 Failed to load resource: the server responded with a status of 406

2017-11-19 14:38 1286 查看
@ResponseBody & @RequestBody

@RequestBody 将 HTTP 请求正文插入方法中,使用适合的HttpMessageConverter将请求体写入某个对象。

@ResponseBody 将内容或对象作为 HTTP 响应正文返回,使用@ResponseBody将会跳过视图处理部分,而是调用适合HttpMessageConverter,将返回值写入输出流。

@ResponseBody可以标注任何对象,由Srping完成对象——协议的转换

我们看到,短短几行配置。使用@ResponseBody注解之后,Controller返回的对象 自动被转换成对应的json数据,在这里不得不感叹SpringMVC的强大。

昨天在做@ResponseBody返回JSON格式的时候,老是报http 406错误,仔细查看了配置文件,原来是出现了两个配置。导致后面那个失效所致,下面给出简单排查和几种解决方案

出错的大致意思是 :



HTTP Status 406 (不接受) 

->无法使用请求的内容特性响应请求的网页。

其中网上很多资料都是说supportedMediaTypes需要添加application/json;charset=UTF-8,但依然出现406 (Not Acceptable)


一:确保applicationContext-configuration.xml配置了<mvc:annotation-driven>

1     <mvc:annotation-driven>
2         <mvc:message-converters>
3             <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
4                 <property name="supportedMediaTypes">
5                     <list>
6                         <
dee6
value>text/plain;charset=utf-8</value>
7                         <value>text/html;charset=UTF-8</value>
8                         <value>text/json;charset=UTF-8</value>
9                         <value>application/json;charset=utf-8</value>
10                     </list>
11                 </property>
12                 <property name="objectMapper">
13                     <bean class="com.fasterxml.jackson.databind.ObjectMapper">
14                         <property name="dateFormat">
15                             <bean class="java.text.SimpleDateFormat">
16                                 <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss"/>
17                             </bean>
18                         </property>
19                     </bean>
20                 </property>
21             </bean>
22         </mvc:message-converters>
23     </mvc:annotation-driven>


 


二:原来springMvc使用@ResponseBody,如果返回的是json结果,需要添加jackson的jar包的依赖

1 <dependency>
2     <groupId>org.codehaus.jackson</groupId>
3     <artifactId>jackson-core-asl</artifactId>
4     <version>1.9.13</version>
5 </dependency>
6     <dependency>
7         <groupId>org.codehaus.jackson</groupId>
8         <artifactId>jackson-mapper-asl</artifactId>
9         <version>1.9.13</version>
10     </dependency>
11 </dependencies>
12 <dependency>
13     <groupId>com.fasterxml.jackson.core</groupId>
14     <artifactId>jackson-databind</artifactId>
15     <version>2.8.0</version>
16 </dependency>



三、测试supportedMediaTypes,就算不配置application/json;charset=UTF-8,也可以正常返回结果。

通过以下测试,确保正确返回

1 <property name="supportedMediaTypes">
2     <list>
3         <value>text/plain;charset=utf-8</value>
4         <value>text/html;charset=UTF-8</value>
5         <value>text/json;charset=UTF-8</value>
6         <value>application/json;charset=utf-8</value>
7     </list>
8 </property>




注意:在使用@ResponseBody 返回json的时候,方法参数中一定不能他添加   PrintWriter printWriter,这就画蛇添足了,而且程序会报错

 java.lang.IllegalStateException: getWriter() has already been called for this response
转载于:https://www.cnblogs.com/molao-doing/p/5957562.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐