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
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.jsfunction 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