使用 Java 使用 REST API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37389064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Consuming REST API with Java
提问by Daniyar Alymkulov
I have a management web application located on a remote server. This app was written using a MEAN stack and I have a list of all the RESTful routes necessary to connect to the web app.
我有一个位于远程服务器上的管理 Web 应用程序。这个应用程序是使用 MEAN 堆栈编写的,我有一个连接到 Web 应用程序所需的所有 RESTful 路由的列表。
I am writing a Java client application that needs to send and receive data from this management app. How do connect the client to the web application if I have the server's IP address and REST routes?
我正在编写一个 Java 客户端应用程序,它需要从这个管理应用程序发送和接收数据。如果我有服务器的 IP 地址和 REST 路由,如何将客户端连接到 Web 应用程序?
I imagine that I need to provide a URL connection to the server and the REST API file and then just call the route functions like PUT
and GET
.
我想我需要提供到服务器和 REST API 文件的 URL 连接,然后只调用路由函数PUT
和GET
.
采纳答案by cassiomolin
There are plenty of libraries to consume REST applications in Java nowadays.
现在有很多库可以在 Java 中使用 REST 应用程序。
The standard
标准
The JAX-RS Client 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 Jerseyand RESTEasy.
JSR 339 中javax.ws.rs.client
定义的 JAX-RS 客户端 API(包)是在 Java 中使用 REST Web 服务的标准方法。除了其他的,这个规范是由Jersey和RESTEasy实现的。
JAX-RS vendor specific proxy-based clients
JAX-RS 供应商特定的基于代理的客户端
Both Jerseyand RESTEasyAPIs provide a proxy framework.
The basic idea is you can attach the 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
calling the right low-level client API methods.
基本思想是您可以将标准 JAX-RS 注释附加到接口,然后通过服务器端的资源类实现该接口,同时通过java.lang.reflect.Proxy
调用正确的方法动态生成该接口的实现,在客户端重用相同的接口低级客户端 API 方法。
For more details, check the following:
有关更多详细信息,请检查以下内容:
Other resources
其他资源
There are a few other good options you may consider as alternative to the JAX-RS Client API:
您可以考虑其他一些不错的选择来替代 JAX-RS 客户端 API:
回答by Ken Brittain
I would begin by reading the documentation for Jersey, specifically the Clientportion. You will want to familiarize yourself with the WebTarget
class and it is invoked (example from the documentation):
我将首先阅读Jersey的文档,特别是Client部分。您将需要熟悉WebTarget
该类并调用它(文档中的示例):
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));