提问者:小点点

理解ExceptionHandlerExceptionResolver的困难


我无法理解为什么ExceptionHandlerExceptionResolver会抛出异常。我已经编写了一个自定义@RestControlller建议ExceptionHandler来捕获Spring Boot应用程序抛出的异常。捕获后,我的ExceptionHandler返回来自抛出的异常的消息作为响应。但我仍然从ExceptionHandlerExceptionResolver收到一条日志消息,我不知道为什么。

这里是日志消息:

2022-05-04 10:08:53.043  INFO 17600 --- [nio-8080-exec-3] at.sds.wm.common.CommonExceptionHandler  : Equipment Barbell 660Kg exists already. Either id already taken or name combined with same equipment already saved
2022-05-04 10:08:53.049  WARN 17600 --- [nio-8080-exec-3] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [at.sds.wm.equipment.exceptions.EquipmentExistsAlreadyException: Equipment Barbell 660Kg exists already. Either id already taken or name combined with same equipment already saved]

我首先想到,调用ExceptionHandlerExceptionResolver是因为我的ExceptionHandler抛出了一个异常,但事实并非如此。

关于如何压缩第二个日志的任何提示和技巧,或者有人能解释为什么调用ExceptionHandlerExceptionResolver吗?

下面是调用的ExceptionHandler部分的代码片段:

    @ExceptionHandler(value = {WorkoutExistsAlreadyException.class, EquipmentExistsAlreadyException.class, ExerciseExistsAlreadyException.class})
    @ResponseStatus(code = HttpStatus.CONFLICT)
    public String handleExistsAlreadyException(RuntimeException ex) {
        log.info(ex.getMessage());
        return ex.getMessage();
    }

共1个答案

匿名用户

这与应用程序属性spring有关。mvc。日志解决的异常。这在spring boot中默认设置为false,但如果使用spring boot开发工具,则会切换为true。

因此,在这种情况下,您必须为异常处理程序解析器声明spring.mvc.log-解析-异常=false以停止编写有关异常的警告。

引用的Spring boot文档