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
- <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-8</value>
- </list>
- </Property>
- </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
- <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>
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