Solution to report HTTP Status 415 when processing json with @requestbody annotations in SPRINGMVC

Source: Internet
Author: User
Tags unsupported

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

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.