简体   繁体   English

如果我的库也可能有带有我不知道的订单注释的 ControllerAdvices,我应该在 ControllerAdvice 上使用什么订单值?

[英]What order value do I use on a ControllerAdvice if my libraries may also have ControllerAdvices with Order annotations that I do not know about?

I have a Spring 4 application with multiple ControllerAdvices annotated with @Order(someValue).我有一个 Spring 4 应用程序,其中有多个用 @Order(someValue) 注释的 ControllerAdvices。 In addition, I have discovered a ControllerAdvice in one of my external libraries also annotated with @Order(someValue).此外,我在我的一个外部库中发现了一个 ControllerAdvice,它也用@Order(someValue) 进行了注释。

My understanding is that when a Controller throws an exception, the order of the ControllerAdvices dictates the order in which the ControllerAdvices are searched for that particular exception.我的理解是,当 Controller 引发异常时,ControllerAdvices 的顺序决定了在 ControllerAdvices 中搜索特定异常的顺序。 I am now thinking it is also possible that there are other ControllerAdvices with @Order annotations in my external libraries.我现在认为在我的外部库中也可能有其他带有 @Order 注释的 ControllerAdvices。 However, it is not feasible for me to download all the libraries and search for all the ControllerAdvices and check their Order values.但是,我无法下载所有库并搜索所有 ControllerAdvices 并检查它们的 Order 值。

How do I know what Order value to put on a particular ControllerAdvice if I want it to catch exceptions before other ControllerAdvices?如果我希望它在其他 ControllerAdvice 之前捕获异常,我如何知道在特定 ControllerAdvice 上放置什么 Order 值? Should I be using a different approach for my use case?我应该为我的用例使用不同的方法吗?

See code below.请参阅下面的代码。

I want ExceptionHandlerControllerTwo to capture exceptions after ExceptionHandlerControllerOne and before ExceptionHandlerControllerThree.我希望 ExceptionHandlerControllerTwo 在 ExceptionHandlerControllerOne 之后和 ExceptionHandlerControllerThree 之前捕获异常。

I tried different Order values for ExceptionHandlerControllerTwo.我为 ExceptionHandlerControllerTwo 尝试了不同的 Order 值。 Numbers 1 through 90 appeared to catch exceptions the way I wanted it to.数字 1 到 90 似乎以我想要的方式捕获异常。 There could be other ControllerAdvices in my external libraries that I do not know about.我的外部库中可能还有其他我不知道的 ControllerAdvice。

In my app:在我的应用程序中:

@ControllerAdvice
@Order(0)
public class ExceptionHandlerControllerOne {
   // multiple @ExceptionHandler methods
}
@ControllerAdvice
@Order(80)
public class ExceptionHandlerControllerTwo {
   // multiple @ExceptionHandler methods
}
@ControllerAdvice
@Order(90)
public class ExceptionHandlerControllerThree {
   // multiple @ExceptionHandler methods
}

In an external library:在外部库中:

@ControllerAdvice
@Order(100)
@Slf4j
public class CatchAllExceptionHandlerController {
   // multiple @ExceptionHandler methods
}

You could try to find all @ControllerAdvice -annotated beans and, according to the result, you define the order.您可以尝试查找所有@ControllerAdvice注释的 bean,并根据结果定义顺序。 To find the order, you could have something like:要查找订单,您可以使用以下内容:

ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
scanner.addIncludeFilter(new AnnotationTypeFilter(ControllerAdvice.class));
scanner.findCandidateComponents("org.example") // Change the package name
        .stream()
        .filter(AnnotatedBeanDefinition.class::isInstance)
        .map(AnnotatedBeanDefinition.class::cast)
        .forEach(annotatedBeanDefinition -> {
            System.out.println(
                    annotatedBeanDefinition.getBeanClassName() + ": " +
                    annotatedBeanDefinition.getMetadata().getAllAnnotationAttributes(Order.class.getName())
            );
        });

@Order(Ordered.HIGHEST_PRECEDENCE) would do the trick, which will tell the framework to run the advice marked this HIGHEST_PRECEDENCE firstly. @Order(Ordered.HIGHEST_PRECEDENCE) 可以解决问题,它会告诉框架首先运行标记为 HIGHEST_PRECEDENCE 的建议。

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

相关问题 我有一个gui程序,我不知道要在mouseclick方法中做什么才能使该程序正常工作 - I have a gui program that I do not know what to do in the method mouseclick in order to make the program to work 如何让用户在代码中设置自己的订单号? - What should I do to have the user set their own order number with in my code? 为了显示它,我必须给JDialog什么样的变量 - What kind of variable do I have to give to a JDialog in order to show it 我是否必须在主类中编写每个方法才能使用它(策略模式)? - Do I have to compose every method in my main class in order to use it (Strategy pattern)? 我是否必须更改我的代码才能使用扫描仪才能使 inputmismatch 异常起作用? - Do I have to change my code to use a scanner in order for inputmismatch exception to work? 为什么我必须按相反的使用顺序关闭 JDBC 资源? - Why do I have to close JDBC resources in reverse order of use? 为了将 listAll 用于 Firebase 存储,我需要什么? - What do I need in order to use listAll for Firebase storage? 如何使用同步来订购LinkedBlockingQueue? - How do I use synchronization to order my LinkedBlockingQueue? 我必须以哪种顺序编码? - In which order do I have to code? 我不知道我的Android Studio发生了什么,我没有Google材料的任何组件 - I do not know what is happening with my Android Studio, I do not have any component of Google Material
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM