Spring MVC请求方法’PATCH’不受支持。

huangapple go评论80阅读模式
英文:

Spring MVC Request method 'PATCH' not supported

问题

HTTP PATCH在Spring MVC/Boot中默认没有启用吗?我得到了以下错误:

org.springframework.web.HttpRequestMethodNotSupportedException: 不支持请求方法 'PATCH'
        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);
	}
}

我已经进行了谷歌搜索,但是没有找到可以帮助解决问题的信息。

谢谢。

英文:

Is HTTP PATCH not enabled by default in Spring MVC/Boot? I'm getting the ff error:

org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PATCH' not supported
        at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:213)

For my controller:

@PatchMapping("/id")
public ResourceResponse updateById(@PathVariable Long id, ServletServerHttpRequest request) {

I have my configuration as follows:

.antMatchers(HttpMethod.PATCH, "/products/**").hasRole("MANAGER")
...
configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH"));

I checked the source code of Spring FrameworkServlet.java, there is something special to 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);
	}
}

I googled already but I was not able to find anything that can help resolve my issue.

Thank you.

答案1

得分: 3

我尝试了一个演示的Spring Boot应用程序,补丁按预期工作。

在您的代码中有一个无关的问题...您在updateById方法中使用了@PathVariable(“id”),但URI中没有pathVariable占位符。

英文:

I tried on a demo spring boot application and patch is working as expected.

There is one unrelated issue in your code... You are using @PathVariable("id") in updateById method without having a pathVariable placeholder in the URI.

答案2

得分: 1

我已解决了问题,在我的情况下我犯了一个错误,我写道:

@PatchMapping(params = "/{id}", consumes = "application/json")

而应该是:

@PatchMapping(path = "/{id}", consumes = "application/json")

英文:

i did resolve the issues, in my case i made a mistake, i wrote

@PatchMapping(params = "/{id}", consumes = "application/json")

instead of:

@PatchMapping(path = "/{id}", consumes = "application/json")

答案3

得分: 0

标准的HTTP客户端不支持PATCH请求。

您只需将Apache HTTP客户端添加到您的项目中。如果在类路径中找到它,Spring Boot应该会自动添加它。

https://hc.apache.org/

英文:

The standard HTTP client does not support PATCH requests.

You can just add the apache HTTP client to your project. It should be automatically added by spring boot if it's found on the classpath.

https://hc.apache.org/

答案4

得分: 0

我决定将 @PostMapping 替换为 @RequestMapping,以解决“不支持请求方法 'PATCH'”的问题。

英文:

I decided "Request method 'PATCH' not supported" by replacing @PostMapping with @RequestMapping

huangapple
  • 本文由 发表于 2020年9月24日 03:43:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64035229.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定