使用RestClientBuilder时出现"No RestClientBuilderResolver implementation found!"错误
问题:MicroProfile RestClientBuilder 报错 "No RestClientBuilderResolver implementation found!"
在Payara 6.2023.11 + Jakarta EE 10环境下,使用MicroProfile RestClientBuilder构建客户端时触发错误:No RestClientBuilderResolver implementation found!。小型测试项目中重复相同操作可正常运行,排除依赖冲突可能。
引发问题的代码片段
MyClient myClient = RestClientBuilder.newBuilder() .baseUri(new URI(Url)) .build(MyClient.class);
MyClient接口定义
@Path("/root") @RegisterRestClient @ClientHeaderParam(name = "x-api-key", value = MyKey) public interface MyClient { @POST @Path("/endpoint1") public MyObj getDeviceState(JsonObject body); @POST @Path("/endpoint2") public MyObj getGatewayState(JsonObject body); }
当前依赖配置
<dependency> <groupId>org.eclipse.microprofile.rest.client</groupId> <artifactId>microprofile-rest-client-api</artifactId> <version>3.0</version> </dependency> <dependency> <groupId>org.eclipse.microprofile</groupId> <artifactId>microprofile</artifactId> <version>5.0</version> <type>pom</type> <scope>provided</scope> </dependency>
已尝试的操作
- 更换多个版本的依赖(单独/组合使用)
- 改用
RestClientBuilderResolver.instance().newBuilder()创建Builder,能找到实现但问题仍未解决 - 在小型测试项目复现操作正常,引入主项目所有依赖也无问题,排除依赖冲突
问题原因及解决方法
1. 版本不匹配(核心原因)
Payara 6.2023.11适配MicroProfile 6.0(对应Jakarta EE 10),但当前依赖使用的是MicroProfile 5.0的BOM,版本不兼容导致容器无法找到正确的实现类。
解决:
修改依赖配置,将MicroProfile BOM版本改为6.0,并将API依赖设为provided(容器已内置实现,无需打包进应用):
<dependency> <groupId>org.eclipse.microprofile.rest.client</groupId> <artifactId>microprofile-rest-client-api</artifactId> <version>3.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.eclipse.microprofile</groupId> <artifactId>microprofile</artifactId> <version>6.0</version> <type>pom</type> <scope>provided</scope> </dependency>
2. 类加载器隔离问题
主项目可能存在自定义类加载器配置,导致无法加载Payara内置的RestClientBuilderResolver实现。
解决:
- 检查项目中的
glassfish-web.xml或payara-web.xml,确认类加载器设置未隔离容器类库; - 若为JPMS模块化项目,在
module-info.java中添加模块依赖:requires org.eclipse.microprofile.rest.client;
3. 改用CDI注入规范方式
在Jakarta EE容器中,推荐通过CDI注入获取RestClient实例,避免手动构建的类加载问题:
@Inject @RestClient private MyClient myClient;
若需动态设置baseUri,可结合CDI实例修改:
@Inject private Instance<MyClient> myClientInstance; public MyClient getCustomClient(String baseUrl) { return RestClientBuilder.newBuilder() .baseUri(URI.create(baseUrl)) .build(myClientInstance.get().getClass()); }
4. 检查Payara容器配置
确认Payara服务器的MicroProfile Rest Client功能已启用:
- 登录Payara管理控制台,进入
Configurations->server-config->MicroProfile->Rest Client,确保状态为启用。
内容的提问来源于stack exchange,提问作者Silas Z
相关产品推荐
相关产品推荐

