【转】今天在调试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. [No000052]大蒜怎么吃最美容?吃大蒜的功效及禁忌

    大蒜是最常见的香辛调味料,它被称为天然抗生素,富含大蒜素等多种营养物质和抗氧化剂,具有多种美肤美容作用. 大蒜的5种美容功效 1.除皱.大蒜里的某些成分,有类似维生素E与维生素C的抗氧化.防衰老特性, ...

  2. [No00002B]知乎精选:如果兔子都在拼命奔跑,乌龟该如何前进

    最近看到友人分享的一篇好文章,看了不下三遍,想开了很多的事情…… 在这个世界上永远存在一些比你更加牛的人,无论什么方面.如果把人生比作攀登,也许你穷其一生可以达到一定的高度,但对某些人来说珠峰都不成问 ...

  3. 十步完全理解SQL

    转载于:http://blog.jobbole.com/55086/ 很多程序员视 SQL 为洪水猛兽.SQL 是一种为数不多的声明性语言,它的运行方式完全不同于我们所熟知的命令行语言.面向对象的程序 ...

  4. bytes和bytearray

    bytes bytes是Python 3中特有的,Python 2 里不区分bytes和str. Python 2中 >>> type(b'xxxxx') <type 'str ...

  5. SuperSlidev2.1滑动门

    1.引用jQuery.js 和 jquery.SuperSlide.js 因为SuperSlide是基于jQuery的插件,所以前提必须先引用jQuery,再引用SuperSlide <head ...

  6. PHP程序设计

    ① 在设计评论系统时,关于文章的一些属性,我们最好存放在一个内存缓存中,通过下面的设计每次仅需要查询一次即可获取文章的所有属性 class CommentsDoc{ private static $d ...

  7. Exchange WebSerivce Usage

    //ExchangeService版本为2007SP1 ExchangeService service = new ExchangeService(ExchangeVersion.Exchange20 ...

  8. C#获取文件MD5字符串

    备注 哈希函数将任意长度的二进制字符串映射为固定长度的小型二进制字符串.加密哈希函数有这样一个属性:在计算不大可能找到散列为相同的值的两个不同的输入:也就是说,两组数据的哈希值仅在对应的数据也匹配时才 ...

  9. 用jQuery File Upload做的上传控件demo,支持同页面多个上传按钮

    需求 有这么一个需求,一个form有多个文件要上传,但又不是传统的图片批量上传那种,是类似下图这种需求,一开始是用的swfupload做的上传,但是问题是如果有多个按钮的话,就要写很多重复的代码,于为 ...

  10. 深入理解Message, MessageQueue, Handler和Looper

    做过Android的都知道Message, MessageQueue, Handler和Looper,但知道不代表你理解它们.有时觉得用得很顺手,但Android怎么实现又说不上来,总觉得似懂非懂.不 ...