springkotlinspring-restcontrollerspring-bean

How to use RequestMapping with Functional Bean Definitions in Kotlin and Spring Boot 3?


I am working on upgrading a service from using Spring Boot 2.7 to Spring Boot 3. The project is using Kotlin and all beans are defined via functional bean definitions. Previously all routes worked as expected. After upgrading to Spring Boot 3 and dependencies, all of the routes defined via @ReqestMapping annotations are returning 404s.

We have API routes that are defined as classes with request mappings:

for example:

@RequestMapping("/hello")
class SimpleRoutes() {
    @GetMapping()
    fun helloWorld(): String {
        return "Hello, World!"
    }
}

and then in the beans definition:

bean { SimpleRoutes() }

Prior to the Spring Boot 3 upgrade this worked exactly as expected, returning a 200 and a body of "Hello, World!".

After the Spring Boot 3 upgrade I just get a 404 on these routes, even though the beans appear to be registering without issue.

If I remove the functional bean definition and add a @RestController annotation to the class it again work as expected, but I cannot figure out why the upgraded dependencies broke the ability to use @RequestMapping with functional bean definitions.

We do have a few reactive routes that are defined using the spring webflux route builder and coRouter instead of @RequestMapping annotations, and those continue to work as expected, but unfortunately we are unable to use those for all routes due to limitations with webflux.


Solution

  • An @RequestMapping is only ever useful in an @[Rest]Controller, however before Spring 6 an @RequestMapping on any spring bean would be inspected (with sometimes surprising results).

    In Spring 6 this was fixed. The related issue can be found here (it was part of Spring Framework 6.0.0 M1 already).