【转】今天在调试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. 代码静态解析PMD

    在正式进入测试之前,进行一定的静态代码分析及code review对代码质量及系统提高是有帮助的,以上为数据证明 Pmd 它是一个基于静态规则集的Java源码分析器,它可以识别出潜在的如下问题:– 可 ...

  2. jmeter 中的 HTTP URL Re-writing Modifier

    URL rewriting modifier,因为tomcat的session实现不是通过cookie的,而是通过session id的,就是说,用户登录有了session之后,tomcat就会维护一 ...

  3. 嵌入式linux根文件系统制作

    编译Busybox 从http://www.busybox.net/downloads/下载busybox工具.这里我们下载的上最新版: busybox-1.24.2.tar.bz2 解压Busybo ...

  4. [No000002]大学本科文凭贬值了多少?

    <大学本科文凭贬值了多少?> 朋友开网络公司,招应届毕业生.他们是小本经营,人手本就不多,面试的时候,忙不过来就会拉我过去,假装是公司的面试官.主管什么的,算是滥竽充数.我装模作样面试了几 ...

  5. PHP提升echo, printf, print, file_put_contents等输出方法的效率

    让ECHO变快, 让PHP的请求处理过程, 尽快结束, 之所以ECHO慢, 是在等待”写数据”成功返回, 那么一个比较简单的办法, 就是打开输出缓存, 编辑php.ini output_bufferi ...

  6. php进阶函数

    1,对文件的操作,确保多个进程可以同时读写一个文件(flock函数) flock($hamdle,int $operator) operator的取值,LOCK_SH(共享锁定,读取程序),LOCK_ ...

  7. poj3270

    Cow Sorting Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6750   Accepted: 2633 Descr ...

  8. SQL Server对Xml字段的操作

    T-Sql操作Xml数据 一.前言 SQL Server 2005 引入了一种称为 XML 的本机数据类型.用户可以创建这样的表,它在关系列之外还有一个或多个 XML 类型的列:此外,还允许带有变量和 ...

  9. 我理解的Hanlder--android消息传递机制

    每一个学习Android的同学都会觉得Handler是一个神奇的东西,我也一样,开始我以为我懂了Handler的机制,后来发现自己是一知半解,昨天想想,我能否自己实现一个Handler,让子线程与Ac ...

  10. DataTrigger 绑定枚举

    在触发器中绑定枚举类型: <ControlTemplate.Triggers> <DataTrigger Binding="{Binding CheckStateEnum} ...