SpringMVC_The resource identified by this request is only capable of generating responses with characteristics
今天在调试springMVC的时候,在将一个对象返回为json串的时候,浏览器中出现异常:
The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().
从网上查了下,说是让配置下json转化bean:
- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
- <property name="messageConverters">
- <list>
- <ref bean="mappingJacksonHttpMessageConverter" />
- </list>
- </property>
- </bean>
- <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
- <property name="supportedMediaTypes">
- <list>
- <value>application/json;charset=UTF-8</value>
- </list>
- </property>
- </bean>
配置完运行后还是报上述错误。
应该不是配置原因,从http://forum.spring.io/forum/spring-projects/web/82137-spring-3-and-ajax这个网址中查出以下配置:
- <bean id="messageAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
- <property name="messageConverters">
- <list>
- <!-- Support JSON -->
- <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
- </list>
- </property>
- </bean>
- <bean id="exceptionMessageAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver">
- <property name="messageConverters">
- <list>
- <!-- Support JSON -->
- <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
- </list>
- </property>
- </bean>
其中第一个和之前的配置没有什么差别,但是第二个exceptionMessageAdapter,这个是处理转化json异常的适配器(这个配置对于调试查找问题还是非常有用的)。
当用这个配置替换上面的配置时,浏览器报了如下异常:
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
经查,在http://blog.csdn.net/xplizm/article/details/8205882这篇博文中,提到出现这个问题的原因是因为,@ResponseBody返回的对象中的属性缺少set\get方法。
将返回对象加上set\get方法后,一切ok。
这是xplizm的博文中的总结:
开始总以为是Content-Type或者Accept属性有问题,但找了半天原因才发现这里有个基本的要求:POJO对象要转成Json,则要求POJO中的属性必须都有getter方法,加上getter方法后就正常了 :)
ps:
1.当最后调试正确后,我把上述的配置都注释掉,并加上下面这两个配置:
<context:annotation-config />
<mvc:annotation-driven />
发现也可以正常使用。这说明,可以使用上面的两个简单说明就可以使用springMVC的@ResponseBody注解,并返回json串。
2. 调试配置也是很重要的,调试时报出的异常要比tomcat直接返回的406码要直接的多,更近的接近真相
SpringMVC_The resource identified by this request is only capable of generating responses with characteristics的更多相关文章
- The resource identified by this request is only capable of generating responses with characteristics
[转]今天在调试springMVC的时候,在将一个对象返回为json串的时候,浏览器中出现异常: The resource identified by this request is only cap ...
- 解决The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
SpringMVC中当在浏览器中输入对应的MappingUrl时,报The resource identified by this request is only capable of generat ...
- 浏览器报406 错误:The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers
The resource identified by this request is only capable of generating responses with characteristics ...
- 【406错误】 The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
今天遇到一个奇怪的错误,关于Springmvc的,我明明在Controller方法中写了@ResponseBody,返回一个Map,结果报了406错误. 结果发现,少了一个jar包: 加上去就没事了.
- ztree插件异步加载 使用RESTEasy报错 Only resource methods using @FormParam will work as expected. Resource methods consuming the request body by other means will not work as expected.
在使用ztree插件实现异步加载时遇到后台RESTEasy接收参数问题,查看后台报错: A servlet request to the URI http://localhost:8080/area/ ...
- SpringMVC下Ajax请求的方法,@Responsebody如果返回的是布尔值,ajax不会接到任何回传数据
SpringMVC框架下,如果用ajax向后台请求得方法如果使用@Responsebody返回布尔值的话,ajax得不到任何的回传数据. 但是如果返回String类型,就是正常的. 测试了下代码写得没 ...
- POSTMAN发起请求收到乱码 http 406错误
web前段异常: The resource identified by this request is only capable of generating responses with charac ...
- springmvc整合fastjson
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- @ResponseBody返回不能正确接收
Spring-MVC中@ResponseBody返回Bean到前台接收这么一串代码,还套着HTML标签: The resource identified by this request is only ...
随机推荐
- Resumable Media Uploads in the Google Data Protocol
Eric Bidelman, Google Apps APIs team February 2010 Introduction The Resumable Protocol Initiating a ...
- Rotate String
Given a string and an offset, rotate string by offset. (rotate from left to right) Example Given &qu ...
- OpenWRT
如何查日志:http://blog.appdevp.com/archives/382 logread OpenWrt下创建crontab任务: 确保/etc/crontab/下有名为"roo ...
- 23.跳台阶问题[Fib]
[题目] 一个台阶总共有n级,如果一次可以跳1级,也可以跳2级.求总共有多少总跳法,并分析算法的时间复杂度. [分析] 首先我们考虑最简单的情况.如果只有1级台阶,那显然只有一种跳法.如果有2级台阶, ...
- iOS7 中的新加入的下载类NSURLSession(随ios版本更新而更新)
想详细的了解网络下载的相关知识,要仔细阅读URL Loading System Programming Guide 这里有篇好文章(http://www.shinobicontrols.com/blo ...
- UVA 11827 Maximum GCD (输入流)
题目:传送门 题意:求n个数的最大公约数,暴力不会超时,难点在没有个数控制的输入. 题解:用特殊方法输入. #include <iostream> #include <cmath&g ...
- JavaEE填空与判断
Java EE软件工程师认证考试 试题库- 填空题和选择题 一. 填空题 1. HTML网页文件的标记是__html__,网页文件的主体标记是_body__,标记页面标题的标记是__tit ...
- Docker跨主机通信之路由
一.实验环境: 主机名 主机IP Docker0_IP Docker1 192.168.88.130 172.17.0.1 Docker2 192.168.88.131 172.18.0.1 二.实验 ...
- ThreadGroup分析
本文为转载:http://sunboyyyl.blog.163.com/blog/static/2247381201211531712330/ 在Java中每一个线程都归属于某个线程组管理的一员,例如 ...
- Update startup files更新安装文件
The service request did not complete because access to the service configuration manager was not gra ...