Skip to content

Support Ordered interface for @ControllerAdvice beans #23163

@dicksonleong

Description

@dicksonleong

Affects: 5.1.8


The ControllerAdvice doc states that:

All such beans are sorted via AnnotationAwareOrderComparator, i.e. based on @Order and Ordered, and applied in that order at runtime.

But when I try to order using the Ordered interface, it is not working, although the @Order annotation is working.

Example below:

@ControllerAdvice
//@Order(1)
class TestAdviceFirst : Ordered {
    override fun getOrder(): Int {
        return 1
    }
    @ExceptionHandler
    @ResponseBody
    fun handleException(e: Exception): String {
        return "Handling exception in TestAdviceFirst"
    }
}

@ControllerAdvice
//@Order(2)
class TestAdvice : Ordered {
    override fun getOrder(): Int {
        return 2
    }
    @ExceptionHandler
    @ResponseBody
    fun handleException(e: Exception): String {
        return "Handling exception in TestAdvice"
    }
}

@RestController
class TestController {
    @GetMapping("/exception")
    fun exception(e: Exception) {
        throw Exception()
    }
}

When @Order is commented out, I get the response "Handling exception in TestAdvice". However when I uncomment @Order, then I get the correct order response: "Handling exception in TestAdviceFirst"

There is also a StackOverflow question but it is unanswered: https://stackoverflow.com/questions/51896436/ordered-interface-is-not-taken-into-account-for-controlleradvice-components

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions