相关疑难解决方法(0)

设置多个@ControllerAdvice @ExceptionHandlers的优先级

我有多个用类注释的类@ControllerAdvice,每个类都带有一个@ExceptionHandler方法.

一个处理Exception的目的是如果找不到更具体的处理程序,则应该使用它.

可悲的是,Spring MVC似乎总是使用最通用的case(Exception)而不是更具体的case(IOException例如).

这是人们期望Spring MVC的表现吗?我正在尝试模拟来自Jersey的模式,它评估每个ExceptionMapper(等效组件)以确定它处理的声明类型与已抛出的异常的距离,并始终使用最近的祖先.

exception-handling spring-mvc

68
推荐指数
5
解决办法
4万
查看次数

Spring @ControllerAdvice vs ErrorController

在我的REST服务应用程序中,我计划创建一个@ControllerAdvice类来捕获控制器抛出异常并根据错误类型返回ResponseEntity对象.

但我已经设置了一个@RestController扩展ErrorController来捕获"whitelabel"错误.

这两个人是否以任何方式干涉?在哪种情况下,在设置@ControllerAdvice时会调用ErrorController?

编辑ErrorController代码按要求

@RestController
public class ControllerCustomError implements ErrorController{

    //error json object
    public class ErrorJson {

        public Integer status;
        public String error;
        public String message;
        public String timeStamp;
        public String trace;

        public ErrorJson(int status, Map<String, Object> errorAttributes) {
            this.status = status;
            this.error = (String) errorAttributes.get("error");
            this.message = (String) errorAttributes.get("message");
            this.timeStamp = errorAttributes.get("timestamp").toString();
            this.trace = (String) errorAttributes.get("trace");
        }

    }

    private static final String PATH = "/error";

    @Value("${hybus.error.stacktrace.include}")
    private boolean includeStackTrace = false;

    @Autowired
    private ErrorAttributes errorAttributes;

    @RequestMapping(value = PATH)
    ErrorJson …
Run Code Online (Sandbox Code Playgroud)

error-handling spring exception spring-mvc spring-boot

9
推荐指数
1
解决办法
1435
查看次数