Springmvc_the resource identified by this request was only capable of generating responses with characteristics

Source: Internet
Author: User

Today when debugging SPRINGMVC, an exception appears in the browser when an object is returned as a JSON string:

The resource identified by this request is a capable of generating responses with characteristics not acceptable accord ing to the request "accept" headers ().

Looked up from the internet, said is let the configuration under the JSON conversion 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>


The above error is reported after the configuration has run.
Should not be a configuration reason, the following configuration is isolated from the URL of 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>


There is no difference between the first and previous configurations, but the second exceptionmessageadapter, which is the adapter that handles the conversion JSON exception (this configuration is useful for debugging lookup problems).

When replacing the above configuration with this configuration, the browser reported the following exception:

Org.springframework.web.HttpMediaTypeNotAcceptableException:Could not find acceptable representation

In http://blog.csdn.net/xplizm/article/details/8205882 's blog post, the reason for this problem is that the Set\get method is missing from the properties @ResponseBody the returned object.
After returning the object plus the Set\get method, everything is OK.

This is the summary in Xplizm's blog post:

Beginning always thought is Content-type or accept property has a problem, but found a half-day reason to find here there is a basic requirement: Pojo object to be converted to JSON, it is required Pojo property must have getter method, plus getter method after normal:)

Ps:

1. When the final debugging is correct, I comment out the above configuration, and add the following two configurations:

<context:annotation-config/>
<mvc:annotation-driven/>

The discovery can also be used normally. This means that you can use the two simple instructions above to springmvc the @responsebody annotation and return the JSON string.

2. Debug configuration is also very important, debug times out of the exception than the Tomcat directly returned 406 yards to be more direct, closer to the truth

Springmvc_the resource identified by this request was only capable of generating responses with characteristics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.