我在 Spring 中創建了 REST API,它返回正文請求中作為 JSON 傳遞的值以及一些附加數據。使用spring-boot一切正常運行應用程序時。每次我提出請求時,我都會收到期望值。我的目標是將此應用程序部署在Tomcat端口上8080。部署應用程序后,Tomcat所有POST請求都被拒絕,我收到以下錯誤:{ "timestamp": "2019-07-11T12:33:41.877+0000", "status": 405, "error": "Method Not Allowed", "message": "Request method 'GET' not supported", "path": "/test/"}問題是我正在POST使用POSTMAN. 這是請求正文的樣子:{ "username":"somebody"}我的API代碼:@RestControllerpublic class Test{ @RequestMapping(value = "/", method = RequestMethod.POST) String token(@RequestBody RequestBodyData requestBody) { return "hello" + requestBody.getUsername(); }}為什么我會收到此錯誤?我應該更改 Tomcat 配置還是我的代碼有問題?編輯 1 我還在WildFly服務器上部署了這個應用程序,一切都按我預期的那樣工作。我正確使用 Postman,問題是 Tomcat 或項目配置。
2 回答

牛魔王的故事
TA貢獻1830條經驗 獲得超3個贊
您需要將請求從GET更改為POST。您從請求中返回的錯誤消息證明了這一點:
"error": "Method Not Allowed",
"message": "Request method 'GET' not supported",
閱讀錯誤(通常)可以解釋錯誤。

呼如林
TA貢獻1798條經驗 獲得超3個贊
將端點從 更改為/和/user/call from tomcat as /test/user/
( /appNameAsPerTomcat/user/ )
@RestController
public class Test{
@RequestMapping(value = "/user/", method = RequestMethod.POST)
String token(@RequestBody RequestBodyData requestBody) {
return "hello" + requestBody.getUsername();
}
}
添加回答
舉報
0/150
提交
取消