[英]Ambiguous @ExceptionHandler method for HttpMessageNotReadableException
In my @RestController
I'm successfully handling JSONParse exceptions coming from @RequestBody (for example, a String wrongly entered into an Integer field).在我的
@RestController
,我成功处理了来自@RequestBody 的JSONParse 异常(例如,一个字符串错误地输入到Integer 字段中)。 This is the code:这是代码:
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler({ HttpMessageNotReadableException.class })
public ValidationError handleException(HttpMessageNotReadableException ex) {
if (ex.getCause() instanceof InvalidFormatException) {
...
} else {
throw ex;
}
}
Now I want to move this to a @ControllerAdvice
to be used by many controllers.现在我想把它移到一个
@ControllerAdvice
以供许多控制器使用。 Here it is:这里是:
@Order(Ordered.HIGHEST_PRECEDENCE)
@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler {
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler({ HttpMessageNotReadableException.class })
public ValidationError handleException(HttpMessageNotReadableException ex) {
if (ex.getCause() instanceof InvalidFormatException) {
...
} else {
throw ex;
}
}
But Spring complains with the following: Ambiguous @ExceptionHandler method mapped for [class org.springframework.http.converter.HttpMessageNotReadableException]: {public Object foo.bar.RestExceptionHandler.handleException(org.springframework.http.converter.HttpMessageNotReadableException), public final org.springframework.http.ResponseEntity org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler.handleException(java.lang.Exception,org.springframework.web.context.request.WebRequest) throws java.lang.Exception}
But Spring complains with the following:
Ambiguous @ExceptionHandler method mapped for [class org.springframework.http.converter.HttpMessageNotReadableException]: {public Object foo.bar.RestExceptionHandler.handleException(org.springframework.http.converter.HttpMessageNotReadableException), public final org.springframework.http.ResponseEntity org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler.handleException(java.lang.Exception,org.springframework.web.context.request.WebRequest) throws java.lang.Exception}
I can't override ResponseEntityExceptionHandler.handleException
because it's final.我不能覆盖
ResponseEntityExceptionHandler.handleException
因为它是最终的。 What other options are there?还有哪些其他选择?
Using Spring Boot 2.4.3.使用 Spring 启动 2.4.3。
I can't override ResponseEntityExceptionHandler.handleException because it's final
我不能覆盖 ResponseEntityExceptionHandler.handleException 因为它是最终的
You're supposed to override the protected ResponseEntity<Object> handleHttpMessageNotReadable(...)
method instead for custom error handling.您应该重写
protected ResponseEntity<Object> handleHttpMessageNotReadable(...)
方法,而不是自定义错误处理。
You should not extend from ResponseEntityExceptionHandler class.您不应从 ResponseEntityExceptionHandler class 扩展。 Check this answer https://stackoverflow.com/a/71119336/4757784
检查这个答案https://stackoverflow.com/a/71119336/4757784
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.