JSON post to Spring Controller

前端 未结 5 1494
清酒与你
清酒与你 2020-12-24 08:50

Hi I am starting with Web Services in Spring, so I am trying to develop small application in Spring + JSON + Hibernate. I have some problem with HTTP-POST. I created a metho

5条回答
  •  不知归路
    2020-12-24 09:20

    Convert your JSON object to JSON String using

    JSON.stringify({"name":"testName"})

    or manually. @RequestBody expecting json string instead of json object.

    Note:stringify function having issue with some IE version, firefox it will work

    verify the syntax of your ajax request for POST request. processData:false property is required in ajax request

    $.ajax({ 
        url:urlName,
        type:"POST", 
        contentType: "application/json; charset=utf-8",
        data: jsonString, //Stringified Json Object
        async: false,    //Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation
        cache: false,    //This will force requested pages not to be cached by the browser  
         processData:false, //To avoid making query String instead of JSON
         success: function(resposeJsonObject){
            // Success Action
        }
    });
    

    Controller

    @RequestMapping(value = urlPattern , method = RequestMethod.POST)
    
    public @ResponseBody Test addNewWorker(@RequestBody Test jsonString) {
    
        //do business logic
        return test;
    }
    

    @RequestBody -Covert Json object to java

    @ResponseBody - convert Java object to json

提交回复
热议问题