问题描述
Spring MVC / Boot中默认情况下是否未启用HTTP PATCH?我收到了ff错误:
org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PATCH' not supported
at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:213)
对于我的控制器:
@PatchMapping("/id")
public ResourceResponse updateById(@PathVariable Long id,ServletServerHttpRequest request) {
我的配置如下:
.antMatchers(HttpMethod.PATCH,"/products/**").hasRole("MANAGER")
...
configuration.setAllowedMethods(List.of("GET","POST","PUT","OPTIONS","DELETE","PATCH"));
我检查了Spring FrameworkServlet.java
的源代码,PATCH有一些特殊之处:
@Override
protected void service(HttpServletRequest request,HttpServletResponse response)
throws servletexception,IOException {
HttpMethod httpMethod = HttpMethod.resolve(request.getmethod());
if (httpMethod == HttpMethod.PATCH || httpMethod == null) {
processRequest(request,response);
}
else {
super.service(request,response);
}
}
我已经在Google上进行了搜索,但是找不到任何可以解决我问题的信息。
谢谢。
解决方法
我尝试了一个演示版Spring Boot应用程序,并且补丁按预期工作。
您的代码中存在一个不相关的问题...您正在0
方法中使用@PathVariable("id")
,而URI中没有updateById
占位符。
标准的HTTP客户端不支持PATCH请求。
您可以仅将apache HTTP客户端添加到您的项目中。如果在类路径中找到它,则应该由Spring Boot自动添加。