我正在嘗試一個POST請求,POSTMAN但即使它到達了 tomcat 服務器節點,我的localhost_access.log檔案中也會出現以下錯誤
"POST /app/MyService/myControllerMethod HTTP/1.1" 404 1010
我的控制器類是這樣的:
@Controller("myServicecontroller")
@RequestMapping({"/MyService"})
public class MyServiceController {
@RequestMapping(value = {"myControllerMethod"}, method = {RequestMethod.POST})
public String myControllerMethodBackgroundCallBack(HttpServletRequest httpReq,
@RequestBody String request) {
// rest piece of code
}
}
現在我的郵遞員 curl 我正在嘗試使用空資料(也嘗試了一些值)但得到了 404 以上的錯誤回應
curl --location --request POST 'http://my-ip-address:8080/app/MyService/myControllerMethod' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--header 'My-Header-One: lskdnansdlknalkasndl' \
--header 'My-Header-Two: sadasdsa' \
--data-raw '{}'
我究竟做錯了什么?(app在上面的 url 中是我的服務,在其他請求中運行良好)
當我嘗試使用以下代碼時,同樣的事情它能夠達到 api 200
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url);
postMethod.setRequestBody(requestString);
httpClient.setConnectionTimeout(httpReadTimeOut);
httpClient.executeMethod(postMethod);
uj5u.com熱心網友回復:
我已經成功復制了這個問題并找到了根本原因。
根本原因
@ResponseBodymyControllerMethodBackgroundCallBack方法中缺少注釋。
使固定
@RequestMapping(value = {"myControllerMethod"}, method = {RequestMethod.POST})
@ResponseBody
public String myControllerMethodBackgroundCallBack(HttpServletRequest httpReq,
@RequestBody String request) {
// rest piece of code
}
}
為什么?
@ResponseBody注釋需要@Controller注釋,如果使用@RestController注釋,則@ResponseBody不需要注釋。
簡而言之-
@RestController= @Controller @ResponseBody
您可以在此處閱讀更多資訊@Controllerhttps://medium.com/@akshaypawar911/java-spring-framework-controller-vs-restcontroller-3ef2eb360917@RestController
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/524251.html
標籤:弹簧靴邮政邮差
