简体   繁体   中英

Spring MVC @RequestBody not working with jquery ajax?

Here is my ajax request

var dataModel = {name1:"value1", name2:"value2"};

$.ajax({
              url: "/testURL",
              type: "POST",
              async: false,
              contentType: "application/json",
              data: dataModel,
              success: function(response) {

              }

    })

Here is my relevant snippet from spring xml

<annotation-driven>
        <!-- Message converters are added to use custom object mapper in  MappingJackson2HttpMessageConverter.
            StringHttpMessageConverter is required to avoid MappingJackson2HttpMessageConverter from converting a string into json.
        -->
        <message-converters>
            <beans:bean
                class="org.springframework.http.converter.StringHttpMessageConverter">
            </beans:bean>
            <beans:bean
                class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <beans:property name="objectMapper" ref="jacksonObjectMapper"/>
            </beans:bean>
        </message-converters>
    </annotation-driven>

Here is my controller mapping

    @RequestMapping(value = "/testURL", method = { RequestMethod.POST })
    public String add(HttpServletRequest request, @RequestBody CustomObject customObject) throws Exception {}

But My request does not even reach to controller. As soon as I remove @RequestBody CustomObject customObject it works. But I want to map the json request to CustomObject with @RequestBody which is not happening . Not sure what i am missing here ?

In fact when I inspect request.getParameterMap() it displays empty but as soon as I remove contentType: "application/json" I see parameter map gets populated but still then get below error

`The server refused this request because the request entity is in a format not supported by the requested resource for the requested method`

Here is my CustomObject definition

public class CustomObject implements Serializable {

private static final long serialVersionUID = 1L;
private String name1;
private String name2;

//getters and setters
}

Already gone through JQuery, Spring MVC @RequestBody and JSON - making it work together but did not help

In fact when I inspect request.getParameterMap() it displays empty but as soon as I remove contentType: "application/json"

That is right. Reason is with contentType: "application/json" jquery internally convert the data into string. so there is no request parameter. Without contentType: "application/json" , default contentType' is form data . So data sent is converted to request parameters based on delimiters contentType' is form data . So data sent is converted to request parameters based on delimiters & and =`

Also try data: JSON.stringify(dataModel) , it should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM