Julez
Julez

Reputation: 1050

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.

Upvotes: 2

Views: 12665

Answers (4)

Us3rL0sT
Us3rL0sT

Reputation: 65

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

Upvotes: 0

tio_hecro
tio_hecro

Reputation: 127

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

Upvotes: 1

Sahil Gupta
Sahil Gupta

Reputation: 2176

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.

Upvotes: 4

MrBrightside
MrBrightside

Reputation: 119

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/

Upvotes: 0

Related Questions