報錯信息如下:
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]
@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";
}
}
親測有效