Reputation: 395
I got this error when making an ajax request to my WebService : HTTP Status 415 - Unsupported Media Type I tried to add the good MediaType (Text/Html, i think), but it doesn't work. I have still this error. What could this be, do you think ? Thank you !
My request :
$(document).on('submit','.form-add-edit',function(e){
e.preventDefault();
var idDisruptive = $(e.target).find('input[name=idDisruptive]').val();
var url = "api/disruptive";
var method = "POST";
if (idDisruptive){
url = url + '/' + idDisruptive;
method = "PUT";
}
$.ajax({
url: url,
method : method,
data : getDisruptiveParams(),
success : function (response){
console.log('EDIT')
console.log(response);
//editDisruptive(response);
},
error : function(response){
console.log('EDIT ERROR')
console.log(response);
}
});
});
The Web Service :
@Stateless
@Path("disruptive")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_HTML, MediaType.APPLICATION_XML, MediaType.APPLICATION_FORM_URLENCODED})
@Produces({MediaType.APPLICATION_JSON})
public class DisruptiveFacadeREST extends AbstractFacade<Disruptive> {
@PersistenceContext(unitName = "StoryGeneratorPU")
private EntityManager em;
public DisruptiveFacadeREST() {
super(Disruptive.class);
}
@POST
@Override
public void create(Disruptive entity) {
super.create(entity);
}
@PUT
@Path("{id}")
public void edit(@PathParam("id") Integer id, Disruptive entity) {
super.edit(entity);
}
@Override
protected EntityManager getEntityManager() {
return em;
}
}
Upvotes: 1
Views: 906
Reputation: 209112
You need to set the content-type on the jQuery request. If you don't, it will default to application/x-www-form-urlencoded
. And just because you add @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
doesn't mean that JAX-RS will not how to convert the form data to Disruptive
. There need to be a MessageBodyReader
to handle that conversion, which there isn't. Same goes for MediaType.TEXT_HTML
. Just adding that means nothing if there is no converter to handle the conversion. Remove those two. What you want is to handle JSON conversion, and there should already be a MessageBodyReader
included in the EE server that will convert JSON data to arbitrary POJOs.
So for the jQuery, just add
$.ajax({
contentType: 'application/json'
})
That should solve the problem.
Upvotes: 2