簡體   English   中英

對 Spring Boot Endpoint 的 Ajax 請求無法讀取 HTTP MSG

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

我有一個 ajax 請求將數據傳遞到 Spring Boot 端點。 但是我收到以下錯誤:

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)

這是我的 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);
                    }
                });

這是我的 Spring Boot 端點:

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

如果我將其更改為 POST,它會給我一個錯誤,提示我無法發布到 GET 並且在線閱讀人們建議更改為 GET。 如果您有任何建議,請讓我知道。 我有一種感覺,我在這里缺少一個核心概念。 謝謝。 我會嘗試任何建議並發布更新。

基本上,您正在嘗試向准備接受 GET 的人發布一些內容。 這就像和一個只會說意大利語的人說英語一樣......他們無法相互理解。

無論您的原因是什么,您都必須讓您的客戶端和服務器使用相同的語言,並使用相同的隧道......如果您的客戶端 POST,您的服務器必須接受 POST。 如果您的客戶端 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
}

如果你想接受 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
}

因此,如果您希望能夠檢索@RequestBody ,則必須 POST 。

但是,以更面向 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
}

希望它會幫助你

@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