錯誤場景:今天使用SpringBoot編寫登入和注冊的時候一直報錯org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported,405,我當時就奇怪了為什么我用Post方法,為什么會出現一個Get請求不被支持
關鍵代碼:
靜態頁 <body> <form action="/manager/login" method="post"> 用戶名:<input name="manname" type="text"/><br> 密碼:<input name="manpass" type="password"> <br> <input type="submit" value="登錄"> </form> </body> <body> <form action="/manager/regist" method="post"> 用戶名:<input name="manname" type="text"/><br> 密碼:<input name="manpass" type="password"> <br> <input type="submit" value="注冊"> </form> </body>
看了很多博客,各種解決方法
版本問題【這個不好評價】
把POST方法改為Get方法【這個我已經無力吐槽了,POST和GET方法的區別,不懂的萌新自己去查】
最后還是別人的一句評論讓我解決了這個方法
將@POSTMapping改為@RequestMapping
附加,一個為什么勉強可以解釋為什么可以解決這個問題的原因
@GetMapping
用於將HTTP GET請求映射到特定處理程序方法的注釋。具體來說,@GetMapping是一個作為快捷方式的組合注釋
@RequestMapping(method = RequestMethod.GET)。
@PostMapping
用於將HTTP POST請求映射到特定處理程序方法的注釋。具體來說,@PostMapping是一個作為快捷方式的組合注釋@RequestMapping(method = RequestMethod.POST)。
@RequestMapping:
一般情況下都是用@RequestMapping(method=RequestMethod.),因為@RequestMapping可以直接替代以上兩個注解,但是以上兩個注解並不能替代@RequestMapping,@RequestMapping相當於以上兩個注解的父類!