Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to register @RestControllerAdvice Class via WebTestClient [SPR-16520] #21063

spring-projects-issues opened this issue Feb 20, 2018 · 4 comments
in: web type: enhancement


Copy link

@spring-projects-issues spring-projects-issues commented Feb 20, 2018

kabubu opened SPR-16520 and commented

Given rest controller:

public class MyController {

    @GetMapping(value = "/errors")
    public Flux<Object> getErrors() {
        return Flux.error(new NumberFormatException());

controlleradvice code:

public class GlobalHandler{

    public ResponseEntity<String> hadleEx(NumberFormatException ex) {
       return ResponseEntity.badRequest().build();

and test:

public class SimpleTest{

    WebTestClient webTestClient;

    public void init() {
         webTestClient = WebTestClient
                .bindToController(new MyController())

    public void getErrors() {

Despite invoking controllerAdvice() method on WebTestClient an @ExceptionHandler located on ControllerAdvice doesnt work and test ends with " No content
< 500 Internal Server Error < Content-Type: [application/json;charset=UTF-8]
Content not available yet"

If we put @ExceptionHandler in @RestController body everything works fine.

Affects: 5.0.4

Referenced from: commits bb8cddd, 01d9475

Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 21, 2018

Rossen Stoyanchev commented

This is because the @ControllerAdvice is registered as a class, and not as an instance (as expected). Changing it to new GlobalHandler works. I've added assertions to ensure this is flagged with an IllegalArgumentException.

Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 22, 2018

kabubu commented

Thank you for help and quick answer! putting assertions there is a good idea.

Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 25, 2018

Juergen Hoeller commented

Since we have a test of our own (in the performance profile: see which registered Class instances before (just as a dummy but we still tolerated that registration), I've revised this to turn registered Class arguments into instances on the fly rather than rejecting them... which should usually match the user's intention there.

Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 26, 2018

Rossen Stoyanchev commented

Good idea.

@spring-projects-issues spring-projects-issues added type: enhancement in: web labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 5.0.5 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
in: web type: enhancement
None yet

No branches or pull requests

2 participants