【转】今天在调试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的更多相关文章

  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. c语言结构体小知识

    引自:http://c.biancheng.net/cpp/html/88.html 结构体在内存中是连续存储的 struct stu{ char *name; //姓名 int num; //学号 ...

  2. 第14章 位图和位块传输_14.4 GDI位图对象(2)

    14.4.7 在位图上绘图 (1)在内存设备环境中绘图(与真实DC不同的是,内存DC的显示表面是个位图) (2)GetTextExtentPoint32函数:用于确定文本字符串的像素大小.(此大小就是 ...

  3. C# 事件编程在游戏开发的应用

    2D碰撞检测:http://wenku.baidu.com/view/45544cfcfab069dc50220145.html 1.Action System.Action 等于快捷创建一个委托 2 ...

  4. IOS-利用AFNetworking监听网络状态

    网络环境检测:检测用户当前所处的网络状态 效果图 1.当蜂窝和wifi同时关闭时候  显示为不可达(AFNetworkReachabilityStatusNotReachable)状态 2.打开蜂窝移 ...

  5. PAT 1015. 德才论 (25) JAVA

    宋代史学家司马光在<资治通鉴>中有一段著名的"德才论":"是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人.凡取人之术,苟不得圣人,君子 ...

  6. 用opencv的traincascade训练检测器

    #1,准备正负样本 正样本:可以一张图片上多个sample,也可以一张图片单独成一个sample,准备多个sample.生成描述文件如下所示: 负样本:只要不含正样本,任意图片都可以作为负样本,但是最 ...

  7. mybatis 3.2.8 + log4j2.0.2 控制台输出sql语句

    mybatis3.2.7有一个bug,使用log4j2 (2.0.2)版本时,会找不到类 ,导致启动失败,详见 https://github.com/mybatis/mybatis-3/issues/ ...

  8. Android -- Adapter

    体系 public interface Adapter----0层(表示继承体系中的层次)  public interface ExpandableListAdapter---(无所谓层次因为没有其他 ...

  9. WPF ListView和ListBox等双击事件问题

    上两篇文章中说双击行获取不到当前数据对象问题, http://www.cnblogs.com/ligl/p/5636899.html http://www.cnblogs.com/ligl/p/562 ...

  10. 关于BOM

    BOM:浏览器对象模型 (Browser Object Model)主要定义的是JS操作浏览器的方法和属性. 大部分方法都在window下. 常用方法:(JS里面规定如果方法前面是window,win ...