Solution to report HTTP Status 415 when working with JSON in SPRINGMVC using @requestbody and @responsebody annotations

Source: Internet
Author: User

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

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.