2.19. MicroProfile REST Client
MicroProfile REST 客户端仅作为技术预览提供。技术预览功能不包括在红帽生产服务级别协议(SLA)中,且其功能可能并不完善。因此,红帽不建议在生产环境中使用它们。这些技术预览功能可以使用户提早试用新的功能,并有机会在开发阶段提供反馈意见。
如需有关技术预览功能支持范围的信息,请参阅红帽客户门户网站中的技术预览功能支持范围。
JBoss EAP 7.3 支持 MicroProfile REST 客户端 1.4.x,它构建于 JAX-RS 2.1 客户端 API,以提供通过 HTTP 调用 RESTful 服务的类型安全方法。MicroProfile 类型 Safe REST 客户端定义为 Java 接口。利用 MicroProfile REST 客户端,您可以使用可执行代码编写客户端应用。
MicroProfile REST 客户端启用:
- 直观的语法
- 供应商的程序注册
- 供应商声明注册
- 标头声明规格
- 在服务器中传播标头
-
ResponseExceptionMapper - CDI 集成
2.19.1. 直观的语法 复制链接链接已复制到粘贴板!
MicroProfile REST 客户端启用分布式对象通信版本,它也在 CORBA、Java 远程方法调用(RMI)、JBoss 远程方法传递项目和 RESTEasy 中实施。例如,考虑资源:
@Path("resource")
public class TestResource {
@Path("test")
@GET
String test() {
return "test";
}
}
访问 TestResource 类的 JAX-RS 原生方式是:
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 类的调用在使用 类时变得更容易,如调用 TestResource Intfservice.test() 所示。
以下示例是 TestResourceIntf 类的更详细版本:
@Path("resource")
public interface TestResourceIntf2 {
@Path("test/{path}")mes("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