Skip to content

Controller created in configuration class doesn't work in Spring Boot 3 #34813

New issue

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

By clicking “No 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? No Sign in to your account

Closed
tomasz0801 opened this issue Mar 29, 2023 · 2 comments
Closed
Labels
for: external-project For an external project and not something we can fix

Comments

@tomasz0801
Copy link

tomasz0801 commented Mar 29, 2023

I created a simple project with one controller and instead of putting @RestController annotation on the controller class, I am creating it as a bean in the configuration class.
It works in Spring Boot 2.7.10 (and older ones), but after migration to spring boot 3 (I checked it on 3.0.0 and 3.0.5 versions) I am getting 404.
If I remove the bean from the configuration class and add @RestController annotation to controller class, everything works fine, but I would like to have the possibility to e.g. disable the controller from the configuration class level as I used to have.

Link to exemplary project:
https://github.com/tomasz0801/spring-playground
It contains one failing test, but if I switch Spring Boot version to 2.7.10 it passes.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 29, 2023
@scottfrederick
Copy link
Contributor

The difference you're seeing between Spring Boot 2.7 and 3.0 is due to a change in Spring Framework. In Spring Framework 5, a @RequestMapping annotation was enough for Spring to consider the bean as a controller. With Spring Framework 6, a @Controller or @RestController annotation is required to consider the bean as a controller. See spring-projects/spring-framework#22154 (comment) for the details.

To accomplish the level of configuration control that you want, you can add @RestController to the controller class and exclude it (or the entire package it is in) from component scanning.

@scottfrederick scottfrederick closed this as not planned Won't fix, can't repro, duplicate, stale Mar 29, 2023
@scottfrederick scottfrederick added for: external-project For an external project and not something we can fix and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 29, 2023
@tomasz0801
Copy link
Author

Thank you for your detailed answer. I wasn't aware of that change

No Sign up for free to join this conversation on GitHub. Already have an account? No Sign in to comment
Labels
for: external-project For an external project and not something we can fix
Projects
None yet
Development

No branches or pull requests

3 participants