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 capable of generating responses with characteristics not acceptable according to the request "accept" headers ().

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

[html] view plaincopy

  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-8</value>
  12. </list>
  13. </property>
  14. </bean>

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

[html] view plaincopy

  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码要直接的多,更近的接近真相

时间: 2024-12-30 04:06:55

SpringMVC_The resource identified by this request is only capable of generating responses with characteristics的相关文章

解决The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request &quot;accept&quot; headers.

SpringMVC中当在浏览器中输入对应的MappingUrl时,报The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers. 错误的意思是:说是指定的资源已经找到,但它的MIME类型和客户在Accpet头中所指定的不兼容 @Res

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

浏览器报406 错误:The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request &quot;accept&quot; headers

The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers 网上关于这个问题有很多说法,有的说spring mvc版本问题的,有说jackson jar包问题问题的,试了下都不可以, 这里的解决方法参考: https://www.cn

HTTP Status 406 --- not acceptable according to the request &quot;accept&quot; headers

The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers. 使用以下方案解决 springmvc json配置方案 Spring4.1.0 使用<annotation-driven />MVC配置方法. 在pom.xml

spring mvc 4.1 返回json报406错误的解决办法

浏览器访问,报 The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers. 解决办法,检查springmvc的配置文件中有无 <mvc:annotation-driven />

springmvc项目中调用controller方法报406错误

在controller类的方法中添加了注解:@ResponseBody 网页抛出错误: The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers () 网页能够成功的调入该方法,但是返回的json结果不能正常接收,页面抛出以上错误.

Spring MVC中页面向后台传值的几种方式

在学习 Spring Mvc 过程中,有必要来先了解几个关键参数:   @Controller:         在类上注解,则此类将编程一个控制器,在项目启动 Spring 将自动扫描此类,并进行对应URL路由映射.@Controllerpublic class UserAction {    }  @RequestMapping         指定URL映射路径,如果在控制器上配置 RequestMapping  ,具体请求方法也配置路径则映射的路径为两者路径的叠加 常用映射如:Reque

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

web前段异常: The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers 异常描述:客户端无法处理服务器返回的数据特征(格式) 1.在http请求头中通过accept字段定义客户端能够处理的数据格式, 事实上,下图演示的请求能够接

springmvc——url参数传递

在学习 Spring Mvc 过程中,有必要来先了解几个关键参数:    @Controller: 在类上注解,则此类将编程一个控制器,在项目启动 Spring 将自动扫描此类,并进行对应URL路由映射. 1 2 3 4 5 @Controller public class UserAction {     }  @RequestMapping 指定URL映射路径,如果在控制器上配置 RequestMapping  ,具体请求方法也配置路径则映射的路径为两者路径的叠加 常用映射如:Request