4.8. MicroProfile REST 客户端开发
MicroProfile REST 客户端启用分布式对象通信版本,它也在 CORBA、Java Remote Method Invocation (RMI)、JBoss Remoting Project 和 RESTEasy 中实施。例如,考虑资源:
@Path("resource")
public class TestResource {
@Path("test")
@GET
String test() {
return "test";
}
}
以下示例演示了使用 Jakarta RESTful Web Services-native 方法访问 TestResource 类:
Client client = ClientBuilder.newClient();
String response = client.target("http://localhost:8081/test").request().get(String.class);
但是,Microprofile REST 客户端通过直接调用 test () 方法来支持更直观的语法,如下例所示:
@Path("resource")
public interface TestResourceIntf {
@Path("test")
@GET
public String test();
}
TestResourceIntf service = RestClientBuilder.newBuilder()
.baseUrl(http://localhost:8081/))
.build(TestResourceIntf.class);
String s = service.test();
在前面的示例中,对 TestResource 类进行调用会更容易地使用 TestResourceIntf 类,如调用 service.test () 所示。
以下示例是 TestResourceIntf 类的更详细版本:
@Path("resource")
public interface TestResourceIntf2 {
@Path("test/{path}")
@Consumes("text/plain")
@Produces("text/html")
@POST
public String test(@PathParam("path") String path, @QueryParam("query") String query, String entity);
}
调用 service.test ("p", "q", "e") 方法会导致 HTTP 信息,如下例所示:
POST /resource/test/p/?query=q HTTP/1.1
Accept: text/html
Content-Type: text/plain
Content-Length: 1
e
4.8.2. 在 MicroProfile REST 客户端中编程注册提供程序 复制链接链接已复制到粘贴板!
通过 MicroProfile REST 客户端,您可以通过注册提供程序来配置客户端环境。例如:
TestResourceIntf service = RestClientBuilder.newBuilder()
.baseUrl(http://localhost:8081/))
.register(MyClientResponseFilter.class)
.register(MyMessageBodyReader.class)
.build(TestResourceIntf.class);
4.8.3. MicroProfile REST 客户端中的提供程序声明注册 复制链接链接已复制到粘贴板!
通过向目标接口添加 org.eclipse.microprofile.rest.client.annotation.RegisterProvider 注释,以声明性方式注册提供程序,如下例所示:
@Path("resource")
@RegisterProvider(MyClientResponseFilter.class)
@RegisterProvider(MyMessageBodyReader.class)
public interface TestResourceIntf2 {
@Path("test/{path}")
@Consumes("text/plain")
@Produces("text/html")
@POST
public String test(@PathParam("path") String path, @QueryParam("query") String query, String entity);
}
使用注释声明 MyClientResponseFilter 类和 MyMessageBodyReader 类无需调用 RestClientBuilder.register () 方法。
4.8.4. MicroProfile REST 客户端中的标头声明规格 复制链接链接已复制到粘贴板!
您可以使用以下方法为 HTTP 请求指定标头:
- 通过注解一个资源方法参数。
-
按照声明使用
org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam注释。
以下示例演示了使用注解 @HeaderParam 标注其中一个资源方法参数来设置标头:
@POST
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.TEXT_PLAIN)
String contentLang(@HeaderParam(HttpHeaders.CONTENT_LANGUAGE) String contentLanguage, String subject);
以下示例演示了如何使用 org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam 注释设置标头:
@POST
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.TEXT_PLAIN)
@ClientHeaderParam(name=HttpHeaders.CONTENT_LANGUAGE, value="{getLanguage}")
String contentLang(String subject);
default String getLanguage() {
return ...;
}
4.8.5. MicroProfile REST 客户端中的 ResponseExceptionMapper 复制链接链接已复制到粘贴板!
org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper 类是 javax.ws.rs.ext.ExceptionMapper 类的 client-sideverse,它在 Jakarta RESTful Web Services 中定义。ExceptionMapper.toResponse () 方法在服务器端处理 响应 类期间将引发 Exception 类。Response 方法将在客户端上收到的 Exception Mapper.toThrowable ()Response 类将 HTTP 错误状态转换为例外类。
您可以以编程方式或声明性方式注册 ResponseExceptionMapper 类。如果没有注册的 ResponseExceptionMapper 类,默认的 ResponseExceptionMapper 类会将状态为 >= 400 的任何响应映射到 WebApplicationException 类。
4.8.6. 使用 MicroProfile REST 客户端进行上下文依赖项注入 复制链接链接已复制到粘贴板!
使用 MicroProfile REST 客户端时,您必须使用 @RegisterRestClient 类为作为 Jakarta 上下文和依赖项注入(Jakarta 上下文和依赖注入) bean 标注任何接口。例如:
@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 类实施的路径的信息。此信息可以通过可选的 @RegisterProvider 参数 baseUri 提供:
@Path("database")
@RegisterRestClient(baseUri="https://localhost:8080/webapp")
public interface TestDataBase {
@Path("")
@POST
public String getByName(String name);
}
这表示您可以访问位于 https://localhost:8080/webapp 的 TestDataBase 的实现。您还可以使用 MicroProfile 配置向外部提供信息:
<fully qualified name of TestDataBase>/mp-rest/url=<URL>
例如,以下属性表示您可以访问位于 https://localhost:8080/webapp 的 com.bluemonkeydiamond.TestDatabase 类的实现:
com.bluemonkeydiamond.TestDatabase/mp-rest/url=https://localhost:8080/webapp
您可以为 Jakarta 上下文和依赖注入客户端提供许多其他属性。例如,com.mycompany.remoteServices.MyServiceClient/mp-rest/providers,以逗号分隔的、要包含在客户端中的完全限定提供程序类名称的列表。