When I use Springmvc's @requestbody and @responsebody annotations to process JSON data, there is always a 415 error, saying that the submitted data format is not supported, I used jquery Ajax in the page to emit JSON data to the server:
$.ajax ({ type:' post ', URL:' ${pagecontext.request.contextpath}/ Requestjson.action ', contentType:' application/json;charset=utf-8 ', // The data is JSON: ' {' name ': ' Phone ', ' Price ': 9999} ', success:function(data) { alert (data); } });
The contenttype type is also specified, but there are 415
Finally I found out that the jar used was a problem, and the jar I used originally was:
The spring version is 4.3.6, which always appears 415, and finally I replace the jar package with:
Yes, it's a problem between versions.
Page code:
<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Scripttype= "Text/javascript"src= "${pagecontext.request.contextpath}/js/jquery-3.1.1.min.js"></Script><Scripttype= "Text/javascript"> functionRequestjson () {$.ajax ({type:'Post', URL:'${pagecontext.request.contextpath}/requestjson.action', ContentType:'Application/json;charset=utf-8', //data is JSONData:'{"name": "Phone", "Price": 9999}', Success:function(data) {alert (data); } }); } functionResponsejson () {$.ajax ({type:'Post', URL:'${pagecontext.request.contextpath}/responsejson.action', Data:'name= Mobile &price=9999', Success:function(data) {alert (data); } }); }</Script><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Test JSON</title></Head><Body><inputtype= "button"value= "Request is JSON, output or JSON"onclick= "Requestjson ()"/><inputtype= "button"value= "Request is Key/value, output is JSON"onclick= "Responsejson ()"/></Body></HTML>
Jsontestcontroller.java (Controller):
ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.web.bind.annotation.RequestBody;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.ResponseBody;ImportCn.lynu.model.ItemsCustom; @Controller Public classJsontestcontroller {@RequestMapping ("/requestjson.action") Public@ResponseBody itemscustom Requestjson (@RequestBody itemscustom itemscustom) {returnItemscustom; } @RequestMapping ("/responsejson.action") Public@ResponseBody itemscustom Responsejson (Itemscustom itemscustom) {returnItemscustom; } }
Solution to report HTTP Status 415 when working with JSON in SPRINGMVC using @requestbody and @responsebody annotations