简体   繁体   English

HttpMessageNotReadableException 的模棱两可的 @ExceptionHandler 方法

[英]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.

相关问题 模棱两可的@ExceptionHandler方法映射 - Ambiguous @ExceptionHandler method mapped @ExceptionHandler 没有捕获 HttpMessageNotReadableException - @ExceptionHandler doesn't catch HttpMessageNotReadableException 我有一个带有@ExceptionHandler的父类和一个带有它自己版本的子类。 我得到一个模糊的方法错误 - I have a parent class with an @ExceptionHandler and a subclass with it's own version. I am getting an ambiguous method error 为 [类 org.springframework.web.bind.MethodArgumentNotValidException] 映射的不明确的 @ExceptionHandler 方法 - Ambiguous @ExceptionHandler method mapped for [class org.springframework.web.bind.MethodArgumentNotValidException] 在启动 spring 引导应用程序时获取为 MethodArgumentNotValidException 映射的不明确的 @ExceptionHandler 方法 - Getting Ambiguous @ExceptionHandler method mapped for MethodArgumentNotValidException while startup of spring boot application @ModelAttribute 方法处理 @RequestBody 对象抛出 HttpMessageNotReadableException - @ModelAttribute method handles @RequestBody object throws HttpMessageNotReadableException 测试控制器方法无法引发HttpMessageNotReadableException - testing controller method fails throwing HttpMessageNotReadableException 该方法含糊不清 - The method is ambiguous spring mvc @ExceptionHandler方法得到相同的视图 - spring mvc @ExceptionHandler method get same view 发生异常时不会调用@ExceptionHandler方法 - @ExceptionHandler method not getting invoked when exception occurred
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM