I trying to do an ajax cross domain post call to a spring rest web services api’s in a secure context.
From Jquery I can’t set the contentType attribute cos then I have problem with the secure context.
But without the contentType from spring I receive the following response: 415 (Unsupported Media Type)
Spring controller:
@RequestMapping(value = "/all", method = RequestMethod.POST)
@PreAuthorize("hasAnyAuthority('PROF1','SUDO')")
Jquery:
function getAllUsers(){
var obj = {"limit":10,"page":0};
$.ajax({
url:"https://webServerSite/myService/api/v1/user/all",
dataType: 'json',
xhrFields: {
withCredentials: true
},
crossDomain: true,
data:obj,
type: "post",
success:function(json){
var str = JSON.stringify(json);
console.log(str);
},
error:function(xhr, status, error) {
alert(xhr.responseText);
}
});
}
There is a way to disable on Spring the contentType check?
All my data is json and I wish to set that as default avoiding the content-type header checking.
I tryed defining a custom message converter and with the following code by did’t work:
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
//set path extension to true
configurer.favorPathExtension(true).
//set favor parameter to false
favorParameter(false).
//ignore the accept headers
ignoreAcceptHeader(true).
//dont use Java Activation Framework since we are manually specifying the mediatypes required below
useJaf(false).
defaultContentType(MediaType.APPLICATION_JSON).
mediaType("xml", MediaType.APPLICATION_XML).
mediaType("json", MediaType.APPLICATION_JSON);
}
Thanks
2
Answers
I solved adding the following converter on my spring boot application:
In my Application:
Was necessary for cross domain request over siteminder
You can specify content type your controller method accepts using the
@RequestMapping
annotation with theconsumes
field. For you use case you might consider accepting all content types with the following:UPDATE:
You should not disable the header checking, otherwise, the server does not know which content type is incoming and how to properly parse it.
Given your stack trace, I think the error is coming from a different source. You try to parse an incoming
application/x-www-form-urlencoded; charset=UTF-8
to a@RequestBody
which is not possible with Spring.This issue was already answered at a different place: Http Post request with content type application/x-www-form-urlencoded not working in Spring