2.19.7. CDI 集成
在 MicroProfile REST 客户端中,您必须注释任何作为 CDI Bean 管理且具有 @RegisterRestClient
类的接口。例如:
@Path("resource") @RegisterProvider(MyClientResponseFilter.class) public static class TestResourceImpl { @Inject TestDataBase db; @Path("test/{path}") @Consumes("text/plain") @Produces("text/html") @POST public String test(@PathParam("path") String path, @QueryParam("query") String query, String entity) { return db.getByName(query); } } @Path("database") @RegisterRestClient public interface TestDataBase { @Path("") @POST public String getByName(String name); }
此处,MicroProfile REST 客户端实施为 TestDataBase
类服务创建一个客户端,让 TestResourceImpl
类能够轻松访问。但是,它不包括有关 TestDataBase
类实施路径的信息。此信息可以由可选 @Reg- isterProvider
参数 baseUri
提供:
@Path("database") @RegisterRestClient(baseUri="https://localhost:8080/webapp") public interface TestDataBase { @Path("") @POST public String getByName(String name); }
这表示您可以访问 TestDataBase 的实现,地址
为 https://localhost:8080/webapp。您还可以向外部提供以下系统变量信息:
<fully qualified name of TestDataBase>/mp-rest/url=<URL>
例如,以下命令表示您可以访问位于 https://localhost:8080/webapp 的 com.bluemondiamond.TestDatabase
类的实施:
com.bluemonkeydiamond.TestDatabase/mp-rest/url=https://localhost:8080/webapp