【转】今天在调试springMVC的时候,在将一个对象返回为json串的时候,浏览器中出现异常:

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

  从网上查了下,说是让配置下json转化bean:

  1. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  2. <property name="messageConverters">
  3. <list>
  4. <ref bean="mappingJacksonHttpMessageConverter" />
  5. </list>
  6. </property>
  7. </bean>
  8. <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
  9. <property name="supportedMediaTypes">
  10. <list>
  11. <value>application/json;charset=UTF-</value>
  12. </list>
  13. </property>
  14. </bean>

  配置完运行后还是报上述错误。
  应该不是配置原因,从http://forum.spring.io/forum/spring-projects/web/82137-spring-3-and-ajax这个网址中查出以下配置:

  1. <bean id="messageAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  2. <property name="messageConverters">
  3. <list>
  4. <!-- Support JSON -->
  5. <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
  6. </list>
  7. </property>
  8. </bean>
  9. <bean id="exceptionMessageAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver">
  10. <property name="messageConverters">
  11. <list>
  12. <!-- Support JSON -->
  13. <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
  14. </list>
  15. </property>
  16. </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的更多相关文章

  1. 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 ...

  2. 解决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 ...

  3. 浏览器报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 ...

  4. 【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包: 加上去就没事了.

  5. 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/ ...

  6. SpringMVC下Ajax请求的方法,@Responsebody如果返回的是布尔值,ajax不会接到任何回传数据

    SpringMVC框架下,如果用ajax向后台请求得方法如果使用@Responsebody返回布尔值的话,ajax得不到任何的回传数据. 但是如果返回String类型,就是正常的. 测试了下代码写得没 ...

  7. POSTMAN发起请求收到乱码 http 406错误

    web前段异常: The resource identified by this request is only capable of generating responses with charac ...

  8. springmvc整合fastjson

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  9. @ResponseBody返回不能正确接收

    Spring-MVC中@ResponseBody返回Bean到前台接收这么一串代码,还套着HTML标签: The resource identified by this request is only ...

随机推荐

  1. USACO1.2Milking Cows

    题目描述 三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶.第一个农民在300秒(从5点开始计时)给他的牛挤奶,一直到1000秒.第二个农民在700秒开始,在 1200秒结束.第三个农民在1500秒开 ...

  2. 2016.11.17 NOI plus day0

    今天很乱乱乱乱 根本不想写代码 玩了一早上了 昨晚失眠了 今天又懵逼了 中午就要走了 明天就要考试了 考完试回来就要补文化课了 现在我的内心很平静 因为已经紧张的冻结了 你知道什么叫彷徨么? 机房里的 ...

  3. 开启云时代,银狐H5游戏云通迅框架解决方案出炉!

    没有时间开发服务器? 不懂服务器开发? 还在为WEB SOCKET烦恼?还在为网络卡,负载承受能力小烦恼? 银狐H5游戏云通迅框架,集成通讯SDK和开放API,1天即可完成 它也是开放平台,提供游戏需 ...

  4. jmeter 函数助手里的P,property的使用

    1.函数助手里的 p及property的使用 ${__P(init,2)} , ${__property(init,start,200)} 可以自行定义变量名称,及变量的默认值 P 变量名为init, ...

  5. 技术专题—Python黑客【优质内容聚合贴】

    作者:坏蛋链接:https://zhuanlan.zhihu.com/p/24645819来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 一.前言 本着知识分享,聚合优 ...

  6. Android保存ArrayList至SharedPreferences

    保存ArrayList至SharedPreferences 其中ArrayList中每个元素为String List<String> environmentList = new Array ...

  7. Angularjs使用的一些特点

    1.函数会影响到全局命名空间 javascript 尽量避免使用全局变量,因为他们容易被其他文件脚本覆盖. angularjs让所有函数的作用域作用在该模块下面,避免了该问题. 2.angularjs ...

  8. 多项式FFT相关模板

    自己码了一个模板...有点辛苦...常数十分大,小心使用 #include <iostream> #include <stdio.h> #include <math.h& ...

  9. PAT 1019. 数字黑洞 (20)

    给定任一个各位数字不完全相同的4位正整数,如果我们先把4个数字按非递增排序,再按非递减排序,然后用第1个数字减第2个数字,将得到一个新的数字.一直重复这样做,我们很快会停在有"数字黑洞&qu ...

  10. POJ 3259 Wormholes (判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46123 Accepted: 17033 Descripti ...