2

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.

Julez
  • 1,010
  • 22
  • 44
  • 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. – Sahil Gupta Sep 24 '20 at 08:56
  • Oh. Good catch! I sure think that's the cause. I missed out the `{}`. Thank you. – Julez Sep 24 '20 at 10:10
  • If that solves your problem. Shall i pop-it out as an answer ? – Sahil Gupta Sep 24 '20 at 11:15
  • Sure and I'll accept it as an answer. – Julez Sep 24 '20 at 15:00

4 Answers4

3

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.

Mario Codes
  • 689
  • 8
  • 15
Sahil Gupta
  • 2,028
  • 15
  • 22
1

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

ikhvjs
  • 5,316
  • 2
  • 13
  • 36
tio_hecro
  • 127
  • 1
  • 5
0

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/

MrBrightside
  • 119
  • 1
  • 9
0

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

Us3rL0sT
  • 3
  • 2
  • 2
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 22 '23 at 14:23