Skip to content

ControllerAdvice order is not preserved in AnnotatedControllerExceptionResolver #830

Closed
@yodono

Description

@yodono

Hello,

When you have many @ControllerAdvice classes, the @Order given to each bean is not respected.

I built a sample project demonstrating this behavior: https://github.com/yodono/spring-graphql-advice-order. Despite one of my classes having an @Order(Ordered.HIGHEST_PRECEDENCE) annotation, it does not run first.

It seems that AnnotatedControllerExceptionResolver is using a ConcurrentHashMap when registering beans in registerControllerAdvice. Later, when resolveException is called, the entries of this Map are iterated over but keeping no reference to the original order.

As a suggestion, there could be an extra call to OrderComparator before iterating over the controllerAdviceCache.

List<Map.Entry<ControllerAdviceBean, MethodResolver>>
    entryList = new ArrayList<>(this.controllerAdviceCache.entrySet());

entryList.sort(Map.Entry.comparingByKey(OrderComparator.INSTANCE));

for (Map.Entry<ControllerAdviceBean, MethodResolver> entry : entryList) {
    ControllerAdviceBean advice = entry.getKey();
    ...
}

Metadata

Metadata

Assignees

Labels

in: webIssues related to web handlingtype: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions