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