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-</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码要直接的多,更近的接近真相;
The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().这个问题加了上面两个配置以后终于解决了,可喜可贺。
转载自csdn:打开 ;
The resource identified by this request is only capable of generating responses with characteristics的更多相关文章
- 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 capabl ...
- 解决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 ...
随机推荐
- Flex4 vs Flex3: Repeater vs DataGroup
repeaters太老土了!如果你过去使用过它,你会发出这种感概.现在,我们终于要摆脱它了.Repeaters不仅有沉重的组件,而且接合使用很不方便.那么,Flex 4中有什么可以帮助我们吗?Data ...
- SQL变量、运算符、分支、循环语句
变量: SQL语言也跟其他编程语言一样,拥有变量.分支.循环等控制语句. 在SQL语言里面把变量分为局部变量和全局变量,全局变量又称系统变量. 局部变量: 使用declare关键字给变量声明,语法非常 ...
- EditText限制输入字符类型的几种方式
最近的项目上需要限制EditText输入字符的类型,就把可以实现这个功能的方法整理了一下: 1.第一种方式是通过EditText的inputType来实现,可以通过xml或者Java文件来设置.假如我 ...
- webpack.optimize.CommonsChunkPlugin插件的使用
方式一,传入字符串参数 new webpack.optimize.CommonsChunkPlugin('common.js'), // 默认会把所有入口节点的公共代码提取出来,生成一个common. ...
- jquery.validate.min.js 用法方法示例
页面html 代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- 033医疗项目-模块三:药品供应商目录模块——供货商药品目录t添加查询功能----------Dao层和Service层和Action层和调试
什么叫做供货商药品目录t添加查询功能?就是说我们前面的博客里面不是说供货商登录后看到了自己供应的药品了么如下: 现在供货商想要往里面添加别的药品,那么这个药品的来源就是卫生局提供的那个Ypxx表(药品 ...
- 【MVC】AJAX+PartialView实现商城首页的楼层加载
使用AJAX实现楼层加载的例子已经非常多,但是html代码大都是手动拼接的,编写不便,而且难以维护. 下面就使用AJAX+PartialView来实现 1.html代码 <!--楼层1开始--& ...
- BZOJ 1024 【SCOI2009】 生日快乐
Description windy的生日到了,为了庆祝生日,他的朋友们帮他买了一个边长分别为 X 和 Y 的矩形蛋糕.现在包括windy,一共有 N 个人来分这块大蛋糕,要求每个人必须获得相同面积的蛋 ...
- 【MySql】存储过程添加事务
存储过程使用SQLException捕获SQL错误,然后处理: 我们可以在MySQL存储过程中捕获SQL错误,然后通过事务判断,回滚(ROLLBACK)还是提交(COMMIT). CREATE PRO ...
- Kafka是分布式发布-订阅消息系统
Kafka是分布式发布-订阅消息系统 https://www.biaodianfu.com/kafka.html Kafka是分布式发布-订阅消息系统.它最初由LinkedIn公司开发,之后成为Apa ...