java - Spring ExceptionHandler for Request method 'POST' not supported
问题描述
I want to customize the response when a caller uses wrong HTTP method.
@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;
}
I am able to catch many error states like malformed JSON input
@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);
}
Now I want to catch when a client uses POST instead of PUT
This generic catcher does not work:
@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);
}
How can I catch and override it?
解决方案
推荐阅读
- node.js - 错误:回调已被调用。当使用异步瀑布并尝试永远运行程序时
- python - 列表不保存其值 - python
- python - 由于 EnvironmentError,spaCy 模型安装失败
- django - 使用 django 序列化程序发布到数据库
- c - 函数参数有例外吗?
- javascript - 如何优化“比特流”以处理 JavaScript 中的输入块?
- c# - 来自响应的 ASP.NET Core MVC 空对象
- python - 不确定为什么对象作为字符串传递
- node.js - 如何将 etcd3 nodejs 客户端用于与标准不同的端口?
- python - 带有时区的 Python 日期,UTC 和 CET 之间的奇怪差异