javaspring-bootspring-mvchttp-patch

Spring MVC Request method 'PATCH' not supported


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.


Solution

  • 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.