解決springboot中只支持get請求,無法支持post請求


報錯信息如下:

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Nov 17 14:42:08 CST 2020
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported
2020-11-17 14:42:08.776  WARN 1928 --- [nio-8890-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported]

1605595943420

1605595968097

@Controller
@RequestMapping(value = "/manage/*") 
public class RouteController {

    @PostMapping(value = "/test")
    @ResponseBody
    public String test(){
        return "success";
    }
}

解決方法如下:

給父級路徑加上method = {RequestMethod.GET,RequestMethod.POST} 使其同時支持get和post方式

@Controller
@RequestMapping(value = "/manage/*",method = {RequestMethod.GET,RequestMethod.POST}) // method = {RequestMethod.GET,RequestMethod.POST}解決無法用post方式提交訪問接口
public class RouteController {

    @PostMapping(value = "/test")
    @ResponseBody
    public String test(){
        return "success";
    }
}

1605596132984

親測有效


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM