使用 REST API 和 Java
Consuming REST API with Java
我有一个位于远程服务器上的管理 Web 应用程序。这个应用程序是使用 MEAN 堆栈编写的,我有一个列表,其中包含连接到网络应用程序所需的所有 RESTful 路由。
我正在编写一个 Java 客户端应用程序,它需要从这个管理应用程序发送和接收数据。如果我有服务器的 IP 地址和 REST 路由,如何将客户端连接到 Web 应用程序?
我想我需要提供到服务器和 REST API 文件的 URL 连接,然后只需调用 PUT
和 GET
等路由函数.
我将从阅读 Jersey, specifically the Client 部分的文档开始。您需要熟悉 WebTarget
class 并调用它(文档中的示例):
ClientConfig clientConfig = new ClientConfig();
clientConfig.register(MyClientResponseFilter.class);
clientConfig.register(new AnotherClientFilter());
Client client = ClientBuilder.newClient(clientConfig);
client.register(ThirdClientFilter.class);
WebTarget webTarget = client.target("http://example.com/rest");
webTarget.register(FilterForExampleCom.class);
WebTarget resourceWebTarget = webTarget.path("resource");
WebTarget helloworldWebTarget = resourceWebTarget.path("helloworld");
WebTarget helloworldWebTargetWithQueryParam =
helloworldWebTarget.queryParam("greeting", "Hi World!");
Invocation.Builder invocationBuilder =
helloworldWebTargetWithQueryParam.request(MediaType.TEXT_PLAIN_TYPE);
invocationBuilder.header("some-header", "true");
Response response = invocationBuilder.get();
System.out.println(response.getStatus());
System.out.println(response.readEntity(String.class));
现在 Java 有很多库可以使用 REST 应用程序。
标准
JAX-RS 客户端 API(javax.ws.rs.client
package), defined in the JSR 339, is the standard way to consume REST web services in Java. Besides others, this specification is implemented by Jersey and RESTEasy。
JAX-RS 供应商特定的基于代理的客户端
两者 Jersey and RESTEasy API 都提供代理框架。
基本思路是您可以附加 standard JAX-RS annotations to an interface, and then implement that interface by a resource class on the server side while reusing the same interface on the client side by dynamically generating an implementation of that using java.lang.reflect.Proxy
调用正确的低级客户端 API 方法。
有关更多详细信息,请查看以下内容:
其他资源
您可以考虑其他一些不错的选择来替代 JAX-RS 客户端 API:
我有一个位于远程服务器上的管理 Web 应用程序。这个应用程序是使用 MEAN 堆栈编写的,我有一个列表,其中包含连接到网络应用程序所需的所有 RESTful 路由。
我正在编写一个 Java 客户端应用程序,它需要从这个管理应用程序发送和接收数据。如果我有服务器的 IP 地址和 REST 路由,如何将客户端连接到 Web 应用程序?
我想我需要提供到服务器和 REST API 文件的 URL 连接,然后只需调用 PUT
和 GET
等路由函数.
我将从阅读 Jersey, specifically the Client 部分的文档开始。您需要熟悉 WebTarget
class 并调用它(文档中的示例):
ClientConfig clientConfig = new ClientConfig();
clientConfig.register(MyClientResponseFilter.class);
clientConfig.register(new AnotherClientFilter());
Client client = ClientBuilder.newClient(clientConfig);
client.register(ThirdClientFilter.class);
WebTarget webTarget = client.target("http://example.com/rest");
webTarget.register(FilterForExampleCom.class);
WebTarget resourceWebTarget = webTarget.path("resource");
WebTarget helloworldWebTarget = resourceWebTarget.path("helloworld");
WebTarget helloworldWebTargetWithQueryParam =
helloworldWebTarget.queryParam("greeting", "Hi World!");
Invocation.Builder invocationBuilder =
helloworldWebTargetWithQueryParam.request(MediaType.TEXT_PLAIN_TYPE);
invocationBuilder.header("some-header", "true");
Response response = invocationBuilder.get();
System.out.println(response.getStatus());
System.out.println(response.readEntity(String.class));
现在 Java 有很多库可以使用 REST 应用程序。
标准
JAX-RS 客户端 API(javax.ws.rs.client
package), defined in the JSR 339, is the standard way to consume REST web services in Java. Besides others, this specification is implemented by Jersey and RESTEasy。
JAX-RS 供应商特定的基于代理的客户端
两者 Jersey and RESTEasy API 都提供代理框架。
基本思路是您可以附加 standard JAX-RS annotations to an interface, and then implement that interface by a resource class on the server side while reusing the same interface on the client side by dynamically generating an implementation of that using java.lang.reflect.Proxy
调用正确的低级客户端 API 方法。
有关更多详细信息,请查看以下内容:
其他资源
您可以考虑其他一些不错的选择来替代 JAX-RS 客户端 API: