请求方法'POST'的Spring ExceptionHandler不支持

时间:2018-12-15 10:51:58

标签: java spring-boot

我想在调用者使用错误的HTTP方法时自定义响应。

@RequestMapping(value = "/simulator/table/{id}/action", method = RequestMethod.PUT)
public GenericResponse doSomething(HttpServletRequest servletRequest, @PathVariable("id") String id, @RequestBody MyRequest request)
        throws BadRequestException, AuthorizationFailedException {
    return response;
}

我能够捕获许多错误状态,例如JSON输入格式错误

@ExceptionHandler({HttpMessageNotReadableException.class})
public ResponseEntity resolveException(HttpServletRequest request, HttpMessageNotReadableException e) {
    log.error("Error when parsing the request {}\n{}", request.getRequestURI(), "a", e);
    GenericResponse response = new GenericResponse("FAILED", error);
    return new ResponseEntity<Object>(response, new HttpHeaders(), HttpStatus.BAD_REQUEST);
}

现在,我想了解客户端何时使用POST而不是PUT

此通用捕获器不起作用:

@ExceptionHandler
public ResponseEntity resolveException(HttpServletRequest request, Exception e) {
    log.error("Error when parsing the request {}\n{}", request.getRequestURI(), "a", e);
    GenericResponse response = new GenericResponse("FAILED", error);
    return new ResponseEntity<Object>(response, new HttpHeaders(), HttpStatus.BAD_REQUEST);
}

如何捕获并覆盖它?

0 个答案:

没有答案