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

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

Comments

@spring-projects-issues
Copy link
Collaborator

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

kabubu opened SPR-16520 and commented

Given rest controller:

@RestController
public class MyController {

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

controlleradvice code:

@RestControllerAdvice 
public class GlobalHandler{

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

and test:

@RunWith(SpringRunner.class)
public class SimpleTest{

    WebTestClient webTestClient;

    @Before
    public void init() {
         webTestClient = WebTestClient
                .bindToController(new MyController())
                .controllerAdvice(GlobalHandler.class)             
                .build();
    }

  @Test
    public void getErrors() {
        this.webTestClient.get().uri("/errors")
                .exchange()
                .expectStatus().isBadRequest();
    }
}

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

@spring-projects-issues
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.

@spring-projects-issues
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.

@spring-projects-issues
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 https://build.spring.io/browse/SPR-PERF-JOB1-1286/test/case/179672969) 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.

@spring-projects-issues
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
Labels
in: web type: enhancement
Projects
None yet
Development

No branches or pull requests

2 participants