My problem is that I have a web form and I want to send this data to a Jersey restFULL Method.
in my Javascript I have this call:
console.log('Sending event creation ---> '+eventInJsonString);
$.ajax({
url: "/Bxip/rest/events/",
// dataType: 'json',
type: 'post',
data: eventInJsonString,
contentType: 'multipart/form-data',
success: function(data) {
console.log("success ", data.response);
},
error: function(data) {
console.log("error ", data.error);
}
});
My jersey method is:
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_HTML)
//Retornará HTTP_STATUS 204 (on success)
public String addEvent(
@FormParam("title") String title,
@FormParam("fechaInicio") String fechaInicio,
@FormParam("lugar") String lugar,
@FormParam("categoria") String categoria,
@FormParam("description") String description,
@FormParam("fechaFin") String fechaFin) throws IOException {
System.out.println("ENTRAAA = " + title + description);
And the result before sending the data:
Sending event creation ---> {"title":"aaaa","description":" aaaaa","fechaInicio":"bbbb","categoria":"Beber y comer","fechaFin":"forever"}
But in the Java server side the parameters are null:
ENTRAAA = nullnull
How can I send @POST to this jersey method from the client?
Several ways to do this. Here you have two possibilities:
To receive @FormParam’s at Jersey you should send the data url encoded in the body of the post.
The ContentType of the request should be
and the content of eventInJsonString should be
To process this in Jersey, you should change the @Consumes type to
This way you will get the @FormParam’s filled.
The problem is that you are trying to send the data in json. If you want to send it that way, you need to change somethings too:
You need to change the request ContentType to
and in Jersey you need to change the @Consumes type to
If you send it that way, you will also need to remove the @FormParam’s from the method: