解决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

亲测有效

posted @ 2020-11-17 15:02  少一点天分sir  阅读(4190)  评论(0)    收藏  举报