Spring's @requestbody is very bull x, and you can convert the submitted JSON directly to the Pojo object.
Just today there is such a demand, use, the results have been reported 415, very headache.
HTTP 415 Error – Unsupported media types (Unsupported media type)
That's what my Angularjs wrote.
$http ({method: "POST", Url:url; Headers: {' content-type ': ' Application/json;charset=utf-8 '}, data:scope. $modelValue}). Success (function (data, Status) { //Success handle Code }). Error (Function (data, status) { //Error handle code});
URL and scope. $modelValue is the code in the project, where it occupies a pit, scope. $modelValue is a JS object that is converted to a JSON string by Angularjs.
Repeatedly look at the Angularjs document, and grab packet analysis, confirm JS no problem.
A search on the internet seems to be the problem of spring, some netizens said need to increase <mvc:annotation-driven/> in the *-servlet.xml, a look at my project did not add, immediately add.
Of course, you also need to add MVC's XML namespace, otherwise the configuration cannot be resolved.
Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xsi:schemalocation= "Http://www.springframework.org/schema /mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "
<mvc:annotation-driven/> Automatically registers defaultannotationhandlermapping and annotationmethodhandleradapter two beans
The Annotationmethodhandleradapter will initialize 7 converters, which can be called by calling Annotationmethodhandleradapter's Getmessageconverts () method to get a collection of converters list
Bytearrayhttpmessageconverter Stringhttpmessageconverter Resourcehttpmessageconverter SourceHttpMessageConverter Xmlawareformhttpmessageconverter Jaxb2rootelementhttpmessageconverter Mappingjacksonhttpmessageconverter
Parsing of JSON is done through the Mappingjacksonhttpmessageconverter converter.
Add <mvc:annotation-driven/> Not yet, you need to find the Jackson bag in the CLASSPATH environment, with maven configured as follows
<dependency> <groupId>org.codehaus.jackson</groupId> <artifactId> jackson-mapper-asl</artifactid> <version>1.9.8</version> <type>jar</type > <scope>compile</scope> </dependency>
Now that the problem is resolved, enclose the spring code
@RequestMapping (value = "/testjson", method = requestmethod.post, consumes = "Application/json") @ResponseBodypublic void Testjson (@RequestBody jsoninfo jsoninfo, httpservletrequest request, httpservletresponse response) { // Handle Jsoninfo Object Instance}
Help from the following, thank the author:)
http://snowolf.iteye.com/blog/1628861
Solution to report HTTP Status 415 when processing json with @requestbody annotations in SPRINGMVC