未捕获的异常:class org.springframework.web.HttpRequestMethodNotSupportedException:不支持请求方法'PUT'

时间:2018-04-02 10:49:23

标签: spring-boot

我在Spring启动时创建了PatchMapping 当我想要修改并使用方法时将其放入

  

======错误:未捕获异常:class org.springframework.web.HttpRequestMethodNotSupportedException:   请求方法'PUT'不受支持==================

如果我使用方法patch,它正常工作。

2 个答案:

答案 0 :(得分:0)

如果要通过执行PUT动词来调用服务,则需要添加spring-boot注释:

    @RequestMapping(path = "/yourURL", method = RequestMethod.PUT)

PATCH与PUT不同。您可以在以下网址找到更多信息:

What is the difference between PUT, POST and PATCH?

答案 1 :(得分:0)

如果要捕获异常,请创建一个实现ErrorController的类:

@Controller
@ControllerAdvice
public class RestErrorHandler implements ErrorController {

@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResponseEntity<RestInvalidParameter> processValidationError(MethodArgumentNotValidException ex) {
        return new ResponseEntity<>(ex, HttpStatus.BAD_REQUEST);
    }
}