I am getting a Request method \'PUT\' not supported
error on hitting a PUT method on a restful API to upload a file.
Following is the
Chek you request mapping
if you declare request mapping in class level @RequestMapping("/api/users")
then if you declare mapping in method level @RequestMapping("/api/users/{id}")
this error could occur.
correct
@RequestMapping("/api/users")
public class {.....
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
update(....)
}
incorrect , might cause PUT not supported error
@RequestMapping("/api/users")
public class {.....
@RequestMapping(value = "/api/users/{id}", method = RequestMethod.PUT)
update(....)
}
To enable the PUT verb you have to add an interceptor that allows that method in the response header.
Something like that:
public class SasAllowOriginInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler)
throws Exception {
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, OPTIONS");
return true;
}
}
I don't know how to add an interceptor using spring boot though, and I would be intersted in knowing it :)
Simply put: your @RequestMapping
doesn't match the request. The regex probably needs some work.
This error happened with me too, I just restarted the server and the error disappeared, the RequestMapping that you've put match the request.
For adding Interceptor
what you should do is implementing WebMvcConfigurer
and overriding addInterceptors method.
@Configuration
public class WebMvcConfigAdapter implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new yourInterceptor());
}
}