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