SPRINGMVC networkerror:415 Unsupported Media Type

Source: Internet
Author: User
Tags unsupported

Little Wolf. There is a problem with the technique of automatically converting a JSON string into an entity in Test SPRINGMVC today

First, the wolf creates a Web project, imports the Springmvc jar file under the Lib directory, and then joins the JSON support class library


Load Jquery.js json2.js in the Webroo directory

The JSP file is written like this:

Springmvc-servlet.xml

<context:component-scan base-package= "Qh.zcy.controller" ></context:component-scan><bean class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "p:prefix="/"p:suffix=". JSP ></bean>

Sendajax.js

function SendAjax2 () {//alert ("test"), var userinforef = new UserInfo ("Stinky", "Zcy"), var jsonstring = json.stringify ( USERINFOREF); alert (jsonstring);//alert (jsonstring); $.ajax ({type: "post", url: "getjsonstring2.zcy?t=" + New Date (). GetTime (), data:jsonstring});} function userinfo (username, password) {this.username = Username;this.password = password;}

Java Controller

@RequestMapping (value= "GetJSONString2", method=requestmethod.post) public String getJsonString2 (@RequestBody Userinfo Userinfo) {System.out.println ("test"); System.out.println (Userinfo.getusername () + "   " +userinfo.getpassword ()); return "Index2";}

Run, quickly error, ("networkerror:415 Unsupported Media type-http://localhost:8080/springmvc_1/getjsonstring2.zcy?t= 1431859248389 ") The Wolf looked for a half-day, will sendajax.js modified as follows:

function SendAjax2 () {//alert ("test"), var userinforef = new UserInfo ("Stinky", "Zcy"), var jsonstring = json.stringify ( USERINFOREF); alert (jsonstring);//alert (jsonstring); $.ajax ({type: "post", url: "getjsonstring2.zcy?t=" + New Date (). GetTime (), Data:jsonstring,contenttype: "Application/json",}); function userinfo (username, password) {this.username = Username;this.password = password;}

Add contenttype: "Application/json".

However, still reported the same mistake, the problem came, did not add Jackson-all-1.9.8.jar

Plus after the same error, why, originally in Springmvc-servlet.xml add a line <mvc:annotation-driven></mvc:annotation-driven>


What's the reason??

First, the Defaultannotationhandlermapping object is responsible for class-level @requestmapping annotations, While Annotationmethodhandleradapter is responsible for the @requestmapping annotation of the method level, if <MVC:ANNOTATION-DRIVEN></MVC is used: annotation-driven> annotations, the defaultannotationhandlermapping and annotationmethodhandleradapter two beans are automatically registered.

Second, the $.post () method does not set contenttype, he has the default, but we are going to pass a JSON-type string. Therefore, change to $.ajax and set ContentType to Application/json, in addition, send the request content do not turn into JSON object, send JSON string directly can

Finally, and most importantly, to add Jackson-all-1.9.8.jar Jacksonjson parsing processing class Library


SPRINGMVC networkerror:415 Unsupported Media Type

Related Article

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.