英文:
How to use RequestMapping with Functional Bean Definitions in Kotlin and Spring Boot 3?
问题
我正在升级一个服务,从使用Spring Boot 2.7升级到Spring Boot 3。该项目使用Kotlin,并且所有的bean都是通过函数式bean定义来定义的。在升级到Spring Boot 3和相关依赖之后,所有通过@ReqestMapping
注解定义的路由都返回404。
我们有一些API路由,它们被定义为带有请求映射的类,例如:
@RequestMapping("/hello")
class SimpleRoutes() {
@GetMapping()
fun helloWorld(): String {
return "Hello, World!"
}
}
然后在bean定义中:
bean { SimpleRoutes() }
在升级到Spring Boot 3之前,这正如预期的那样工作,返回200
和"Hello, World!"作为响应体。
升级到Spring Boot 3后,这些路由只返回404,尽管似乎已成功注册了这些bean。
如果我删除函数式bean定义并在类上添加@RestController
注解,它会再次按预期工作,但我无法弄清楚为什么升级的依赖项会破坏使用@RequestMapping
与函数式bean定义的能力。
我们确实有一些使用Spring WebFlux路由构建器和coRouter
而不是@RequestMapping
注解定义的响应式路由,这些路由仍然按预期工作,但不幸的是,由于WebFlux的限制,我们无法对所有路由都使用它们。
英文:
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.
答案1
得分: 1
@RequestMapping
只在 @[Rest]Controller
中才有用,但在 Spring 6 之前,任何 Spring Bean 上的 @RequestMapping
都会被检查(有时会产生令人惊讶的结果)。
在 Spring 6 中,这个问题已经修复。相关问题可以在这里找到(它已经是 Spring Framework 6.0.0 M1 的一部分)。
英文:
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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论