Reputation: 35
I have an ajax request passing data to a spring boot endpoint. However I'm getting the following error:
Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public java.lang.String com.applicationName.controller.EventController.deleteCalendarEvents(com.applicationName.model.Vacation,javax.servlet.http.HttpSession)
Here's my Ajax Request:
$.ajax({
method: 'POST',
url: '/vacation/deleteEvents',
contentType: 'application/json; charset=utf-8;',
dataType: 'json',
data: JSON.stringify(vacation),
success: function (response) {
if (response !== "OK")
alert(response);
else
console.log(response);
},
error: function (e) {
console.log(e);
}
});
And here's my Spring Boot endpoint:
@RequestMapping(value = "/vacation/deleteEvents", method = RequestMethod.GET)
public String deleteCalendarEvents (@RequestBody Vacation vacation, HttpSession session){
//code
}
If I change this to a POST it gives me an error saying I can't post to a GET and reading online people were suggesting to change to a GET. If you have any suggestions please let me know. I have a feeling i'm missing a core concept here. Thanks. I will try any suggestions and post updates.
Upvotes: 0
Views: 374
Reputation: 2850
Basically, you are trying to POST something to someone that is ready to accept GET. It's like talking english to a person speaking only italian... They can't understant each other.
Whatever your reasons are, you must make your client and server speaking the same language, and using the same tunnel... If your client POST, your server must accept POST. If your client GET, your server must accept GET.
$.ajax({
method: 'POST',
url: '/vacation/deleteEvents',
contentType: 'application/json; charset=utf-8;',
dataType: 'json',
data: JSON.stringify(vacation),
success: function (response) {
if (response !== "OK")
alert(response);
else
console.log(response);
},
error: function (e) {
console.log(e);
}
});
@RequestMapping(value = "/vacation/deleteEvents", method = RequestMethod.POST)
public String deleteCalendarEvents (@RequestBody Vacation vacation, HttpSession session){
//code
}
If you want to accept GET, then your client must send a GET request:
$.ajax({
method: 'GET',
url: '/vacation/deleteEvents',
success: function (response) {
if (response !== "OK")
alert(response);
else
console.log(response);
},
error: function (e) {
console.log(e);
}
});
@RequestMapping(value = "/vacation/deleteEvents", method = RequestMethod.GET)
public String deleteCalendarEvents (HttpSession session){
//code
}
So, you have to POST if you want to be able to retrieve the @RequestBody
.
But then, in a more RESTFul oriented way, you could send a DELETE request:
$.ajax({
method: 'DELETE',
url: `/vacation/${vacation.id}`, // assuming your object vacation has an id field.
success: function (response) {
if (response !== "OK")
alert(response);
else
console.log(response);
},
error: function (e) {
console.log(e);
}
});
@RequestMapping(value = "/vacation/{vacationId}", method = RequestMethod.DELETE)
public String deleteCalendarEvents (@PathVariable int vacationId, HttpSession session){
//code
}
Hope it will help you
Upvotes: 1
Reputation: 21
@RequestMapping(value = "/vacation/deleteEvents", method = RequestMethod.POST)
public String deleteCalendarEvents (@RequestBody Vacation vacation){
//code
}
Upvotes: 0