简体   繁体   English

ExceptionHandlerExceptionResolver的理解难点

[英]Difficulties in understanding ExceptionHandlerExceptionResolver

I have problems understanding on why ExceptionHandlerExceptionResolver throws an Exception.我无法理解ExceptionHandlerExceptionResolver抛出异常的原因。 I have written a custom @RestControllerAdvice ExceptionHandler to catch Exceptions thrown by my Spring Boot application.我已经编写了一个自定义的@RestControllerAdvice ExceptionHandler 来捕获我的 Spring Boot 应用程序抛出的异常。 After the catch my ExceptionHandler returns as a response the message from the thrown exception.在捕获之后,我的 ExceptionHandler 返回来自抛出的异常的消息作为响应。 But I still get a log message from ExceptionHandlerExceptionResolver and I can't figure out why.但是我仍然从ExceptionHandlerExceptionResolver收到一条日志消息,我不明白为什么。

Here the Log message:这里的日志消息:

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]

I first thought, that the ExceptionHandlerExceptionResolver gets called because my ExceptionHandler throws an Exception but that isn't the case.我首先想到,调用ExceptionHandlerExceptionResolver是因为我的ExceptionHandler抛出了异常,但事实并非如此。

Any tips and tricks on how to surpress the second Log, or could someone explain me why the ExceptionHandlerExceptionResolver gets called?关于如何抑制第二个日志的任何提示和技巧,或者有人可以解释为什么调用ExceptionHandlerExceptionResolver吗?

Below my code snippet for the ExceptionHandler part that gets called:在我调用的 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();
    }

This is related with the application property spring.mvc.log-resolved-exception .这与应用程序属性spring.mvc.log-resolved-exception相关。 This is by default set to false in spring-boot but if you use spring-boot-devtools then it is switched to true.这在spring-boot中默认设置为 false,但如果您使用spring-boot-devtools ,则它会切换为 true。

So in that case you have to declare spring.mvc.log-resolved-exception = false for exception handler resolver to stop writting warns about exceptions.因此,在那种情况下,您必须为异常处理程序解析器声明spring.mvc.log-resolved-exception = false以停止编写有关异常的警告。

Referenced Spring boot doc 参考 Spring 启动文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM