Spring MVC Request method 'PATCH' not supported

10.1k Views Asked by At

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.

4

There are 4 best solutions below

0
On BEST ANSWER

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.

1
On

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/

1
On

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

0
On

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")