Spring boot: Request method 'PUT' not supported

前端 未结 5 1625
傲寒
傲寒 2021-01-07 06:12

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

相关标签:
5条回答
  • 2021-01-07 06:39

    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(....)
    
    } 
    
    0 讨论(0)
  • 2021-01-07 06:40

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

    0 讨论(0)
  • 2021-01-07 06:44

    Simply put: your @RequestMapping doesn't match the request. The regex probably needs some work.

    0 讨论(0)
  • 2021-01-07 06:59

    This error happened with me too, I just restarted the server and the error disappeared, the RequestMapping that you've put match the request.

    0 讨论(0)
  • 2021-01-07 07:00

    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());
        }
    }
    
    0 讨论(0)
提交回复
热议问题