您的位置:首页 > 编程语言 > Java开发

雷丰阳Springboot视频培训教程问题解决记录之六——奇怪的Request method 'POST' not supported 405错误

2019-04-16 20:37 531 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Lswx2006/article/details/89341278

最近学习了RESTful服务的构建,在试验@DeleteMapping 删除功能的时候,出现了很奇怪的错误。

点击删除按钮,就会出现如下错误信息:

Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported

There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'POST' not supported.

查了一天文档,没有结果,后来发现教程中Springboot 1.5.10中的WebMvcConfigurerAdapter 方法在2.1X版本中已经不推荐使用了,于是使用了 WebMvcConfigurationSupport 替代。

WebMvcConfigurationSupport  指示SpringBoot 要完全接管SpringMVC的配置,这意味着HiddenHttpMethodFilter 不再自动配置。因此,DELETE 请求被作为POST请求处理。

正确的用法是自己配置 HiddenHttpMethodFilter bean或者使用SpringBoot的自动配置,推荐使用后者。将MyMvcConfig 修改为实现WebMvcConfigurer ,问题解决。

修改后的代码如下:

[code]@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/", "/index.html", "/user/login", "/webjars/**", "/asserts/**");
}

@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
registry.addViewController("/main.html").setViewName("dashboard");
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");

registry.addResourceHandler("/asserts/**")
.addResourceLocations("classpath:/static/asserts/");
}
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐