Quarkus添加Uni方法后RestClient生成失败,求助排查方案
问题排查:Quarkus RestClient Reactive 添加Uni返回值方法后注入失败
问题描述
为ExampleRestClient添加返回值为Uni的方法后,本地运行应用并调用使用该RestClient的路由时,出现以下错误:
Request failed: java.lang.RuntimeException: Error injecting com.example.client.ExampleRestClient com.example.services.BookService.exampleRestClient Failed to generate client for class interface com.fanthreesixty.audience.client.ExampleRestClient : Sub resource type is not a class: io.smallrye.mutiny.Uni
环境信息
- Quarkus版本:
v2.16.6.Final - 使用扩展:
quarkus-rest-client-reactive-jackson(官方支持Uni与阻塞调用)
示例RestClient代码
import io.smallrye.mutiny.Uni; @RegisterRestClient(configKey = "example-core") @RegisterClientHeaders(ExampleClientHeader.class) @ApplicationScoped public interface ExampleRestClient { @GET @Path("/author/{authorId}") @Produces({MediaType.APPLICATION_JSON}) @CacheResult(cacheName = "author") Author getAuthor(@RestPath String authorId); @GET @Path("/books/{bookId}/page-ids") @Produces({MediaType.APPLICATION_JSON}) Uni<Set<UUID>> getBookPagesIds(@RestPath String bookId); @GET @Path("/books/{bookId}/page-ids") @Produces({MediaType.APPLICATION_JSON}) Set<UUID> getBookPagesIdsBlocked(@RestPath String orgId); }
已尝试方案
- 移除
quarkus-rest-client-reactive依赖,问题未解决 - 手动删除依赖并清理IntelliJ缓存,问题依旧
- 搜索相关问题未找到有效方案
- 排查ChatGPT给出的7项检查点,均未解决
排查思路与解决方法
检查@CacheResult注解的兼容性
getAuthor方法使用的@CacheResult注解,在Quarkus 2.16.x版本中对Uni返回值的支持存在限制。暂时注释掉该注解,重新运行应用验证错误是否消失。如果问题解决,说明缓存注解与Uni方法存在冲突,可改用编程式缓存API替代注解式实现。修正重复路径方法的参数不匹配问题
注意到两个路径相同的方法中,阻塞方法getBookPagesIdsBlocked的参数@RestPath String orgId与路径变量bookId不匹配,这会导致客户端生成时元数据混乱。修正参数名与路径变量一致:@GET @Path("/books/{bookId}/page-ids") @Produces({MediaType.APPLICATION_JSON}) Set<UUID> getBookPagesIdsBlocked(@RestPath String bookId);确保Mutiny依赖完整性
虽然quarkus-rest-client-reactive-jackson会自动引入Mutiny,但依赖冲突可能导致Uni类无法被正确识别。可在构建文件中显式添加兼容版本的Mutiny依赖:<!-- Maven --> <dependency> <groupId>io.smallrye.reactive</groupId> <artifactId>smallrye-mutiny</artifactId> <version>1.17.0</version> <scope>provided</scope> </dependency>移除冗余的CDI注解
@RegisterRestClient注解已自动将接口注册为CDI bean,重复添加@ApplicationScoped会导致客户端生成逻辑混乱。移除@ApplicationScoped注解,仅保留@RegisterRestClient和@RegisterClientHeaders。彻底清理并重新构建项目
执行Quarkus的清理构建命令,清除旧编译产物和生成的代理类:# Maven ./mvnw clean install -DskipTests # Gradle ./gradlew clean build -x test
内容的提问来源于stack exchange,提问作者ShowAndTell
相关产品推荐
相关产品推荐

