原文: https://howtodoinjava.com/dropwizard/client-configuration-and-examples/

我们已经使用 dropwizard 构建了 REST API。 现在,让我们构建 REST 客户端,以在整个网络上使用 REST API。 Dropwizard 同时包含 Apache HttpClientJersey 客户端。 让我们来构建它们。

阅读更多: Dropwizard HelloWorld 应用

Maven 依赖

Dropwizard 客户端模块被添加为单独的模块。

  1. <properties>
  2. <dropwizard.version>1.0.0</dropwizard.version>
  3. </properties>
  4. <dependency>
  5. <groupId>io.dropwizard</groupId>
  6. <artifactId>dropwizard-client</artifactId>
  7. <version>${dropwizard.version}</version>
  8. </dependency>

Dropwizard REST 客户端配置

Dropwizard 提供易于声明和使用的 REST 客户端配置。 您需要创建io.dropwizard.client.JerseyClientBuilder实例并为其提供io.dropwizard.setup.Environment参考。

  1. @Override
  2. public void run(Configuration c, Environment e) throws Exception {
  3. //Here we added REST Resource
  4. e.jersey().register(new EmployeeRESTController(e.getValidator()));
  5. //Now we added REST Client Resource named RESTClientController
  6. final Client client = new JerseyClientBuilder(e).build("DemoRESTClient");
  7. e.jersey().register(new RESTClientController(client));
  8. }

要添加 HTTP 客户端,请使用以下类似的步骤:

  1. @Override
  2. public void run(Configuration c, Environment e) throws Exception {
  3. //Here we added REST Resource
  4. e.jersey().register(new EmployeeRESTController(e.getValidator()));
  5. //Now we added REST Client Resource named RESTClientController
  6. final HttpClient client = new HttpClientBuilder(e).build("DemoRESTClient");
  7. e.jersey().register(new RESTClientController(client));
  8. }

HttpClientConfiguration的默认配置如下:

  1. timeout: 500ms
  2. connectionTimeout: 500ms
  3. timeToLive: 1 hour
  4. cookiesEnabled: false
  5. maxConnections: 1024
  6. maxConnectionsPerRoute: 1024
  7. keepAlive: 0s

JerseyClientConfiguration的默认配置如下:

  1. minThreads: 1
  2. maxThreads: 128
  3. gzipEnabled: true
  4. gzipEnabledForRequests: true
  5. //same as HttpClientConfiguration
  6. timeout: 500ms
  7. connectionTimeout: 500ms
  8. timeToLive: 1 hour
  9. cookiesEnabled: false
  10. maxConnections: 1024
  11. maxConnectionsPerRoute: 1024
  12. keepAlive: 0s

Dropwizard REST 客户端资源

现在,当您可以访问 REST 客户端资源RESTClientController.java中的javax.ws.rs.client.Clientorg.apache.http.client.HttpClient时,您可以使用库方法来照常调用 HTTP URI。

  1. package com.howtodoinjava.rest.controller;
  2. import java.util.ArrayList;
  3. import javax.ws.rs.GET;
  4. import javax.ws.rs.Path;
  5. import javax.ws.rs.PathParam;
  6. import javax.ws.rs.Produces;
  7. import javax.ws.rs.client.Client;
  8. import javax.ws.rs.client.Invocation;
  9. import javax.ws.rs.client.WebTarget;
  10. import javax.ws.rs.core.MediaType;
  11. import javax.ws.rs.core.Response;
  12. import com.howtodoinjava.rest.representations.Employee;
  13. @Produces(MediaType.TEXT_PLAIN)
  14. @Path("/client/")
  15. public class RESTClientController
  16. {
  17. private Client client;
  18. public RESTClientController(Client client) {
  19. this.client = client;
  20. }
  21. @GET
  22. @Path("/employees/")
  23. public String getEmployees()
  24. {
  25. //Do not hard code in your application
  26. WebTarget webTarget = client.target("http://localhost:8080/employees");
  27. Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
  28. Response response = invocationBuilder.get();
  29. @SuppressWarnings("rawtypes")
  30. ArrayList employees = response.readEntity(ArrayList.class);
  31. return employees.toString();
  32. }
  33. @GET
  34. @Path("/employees/{id}")
  35. public String getEmployeeById(@PathParam("id") int id)
  36. {
  37. //Do not hard code in your application
  38. WebTarget webTarget = client.target("http://localhost:8080/employees/"+id);
  39. Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
  40. Response response = invocationBuilder.get();
  41. Employee employee = response.readEntity(Employee.class);
  42. return employee.toString();
  43. }
  44. }

在上面的类中,我访问了在 dropwizard HelloWorld 教程中创建的 REST API。

访问 API 之后,我以纯文本形式返回了响应,如下图所示。

Dropwizard 客户端 – Jersey/HTTP 配置和示例 - 图1

DropWizard REST 客户端

我已将客户端资源类的上下文路径设置为/client/,以在逻辑上分离客户端端点和服务端点的 URI。

阅读更多 :

Jersey RESTful 客户端示例

Apache HttpClient GET/POST 请求示例

源码下载

将我的问题放在评论部分。

学习愉快!