简体   繁体   English

对 Spring Boot Endpoint 的 Ajax 请求无法读取 HTTP MSG

[英]Ajax Request to Spring Boot Endpoint Failed to Read HTTP MSG

I have an ajax request passing data to a spring boot endpoint.我有一个 ajax 请求将数据传递到 Spring Boot 端点。 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 请求:

$.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:这是我的 Spring Boot 端点:

    @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.如果我将其更改为 POST,它会给我一个错误,提示我无法发布到 GET 并且在线阅读人们建议更改为 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.我会尝试任何建议并发布更新。

Basically, you are trying to POST something to someone that is ready to accept GET.基本上,您正在尝试向准备接受 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.无论您的原因是什么,您都必须让您的客户端和服务器使用相同的语言,并使用相同的隧道......如果您的客户端 POST,您的服务器必须接受 POST。 If your client GET, your server must accept GET.如果您的客户端 GET,则您的服务器必须接受 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:如果你想接受 GET,那么你的客户端必须发送一个 GET 请求:

$.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 .因此,如果您希望能够检索@RequestBody ,则必须 POST 。

But then, in a more RESTFul oriented way, you could send a DELETE request:但是,以更面向 RESTFul 的方式,您可以发送 DELETE 请求:

$.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希望它会帮助你

@RequestMapping(value = "/vacation/deleteEvents", method = RequestMethod.POST)
public String deleteCalendarEvents (@RequestBody Vacation vacation){
    //code
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM