POST JSON fails with 415 Unsupported media type, Spring 3 mvc

后端 未结 14 1764
执念已碎
执念已碎 2020-11-22 09:46

I am trying to send a POST request to a servlet. Request is sent via jQuery in this way:

var productCategory = new Object();
productCategory.idProductCategor         


        
14条回答
  •  遥遥无期
    2020-11-22 10:16

    I faced a similar issue and this is how I fixed it,

    The problem is due to the conversion process from JSON to Java, one need to have the right run time jackson libraries for the conversion to happen correctly.

    Add the following jars (through dependency or by downloading and adding to the classpath.

    
    org.codehaus.jackson
    jackson-mapper-asl
    1.9.13
    
    
    com.fasterxml.jackson.core
    jackson-databind
    2.5.3
    
    

    This should fix the problem.

    Complete Code:

    function() {
      $.ajax({
        type: "POST",
        url: "saveUserDetails.do",
        data: JSON.stringify({
          name: "Gerry",
          ity: "Sydney"
        }),
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        },
        success: function(data) {
          if (data.status == 'OK')
            alert('Person has been added');
          else
            alert('Failed adding person: ' + data.status + ', ' + data.errorMessage);
    }
    

    and the controller signature looks like this:

    @RequestMapping(value = "/saveUserDetails.do", method = RequestMethod.POST)
    public @ResponseBody Person addPerson( @RequestBody final  Person person) {
    

    Hope this helps

提交回复
热议问题