2

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

0

1 Answer 1

4

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&and=`

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

Sign up to request clarification or add additional context in comments.

1 Comment

I have data in list, when I will do JSON.stringify(data) it will become blank, editForm.credentials=[{attribute:'username', value:'t2'}, {attribute:'password', value:'t2'}]; same is working in postman.$.ajax({url : getContextPath()+ "/abc/update", type : 'POST', data : dataTosend, contentType : 'application/json', success : function(data) {} });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.