<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Dropwizard 客戶端 – Jersey/HTTP 配置和示例 > 原文: [https://howtodoinjava.com/dropwizard/client-configuration-and-examples/](https://howtodoinjava.com/dropwizard/client-configuration-and-examples/) 我們已經使用 dropwizard 構建了 REST API。 現在,讓我們構建 REST 客戶端,以在整個網絡上使用 REST API。 Dropwizard 同時包含 [Apache HttpClient](https://hc.apache.org/httpcomponents-client-ga/quickstart.html) 和 [Jersey 客戶端](https://jersey.java.net/nonav/documentation/latest/user-guide.html#client)。 讓我們來構建它們。 > 閱讀更多: [Dropwizard HelloWorld 應用](//howtodoinjava.com/dropwizard/tutorial-and-hello-world-example/) ## Maven 依賴 Dropwizard 客戶端模塊被添加為單獨的模塊。 ```java <properties> <dropwizard.version>1.0.0</dropwizard.version> </properties> <dependency> <groupId>io.dropwizard</groupId> <artifactId>dropwizard-client</artifactId> <version>${dropwizard.version}</version> </dependency> ``` ## Dropwizard REST 客戶端配置 Dropwizard 提供易于聲明和使用的 REST 客戶端配置。 您需要創建`io.dropwizard.client.JerseyClientBuilder`實例并為其提供`io.dropwizard.setup.Environment`參考。 ```java @Override public void run(Configuration c, Environment e) throws Exception { //Here we added REST Resource e.jersey().register(new EmployeeRESTController(e.getValidator())); //Now we added REST Client Resource named RESTClientController final Client client = new JerseyClientBuilder(e).build("DemoRESTClient"); e.jersey().register(new RESTClientController(client)); } ``` 要添加 HTTP 客戶端,請使用以下類似的步驟: ```java @Override public void run(Configuration c, Environment e) throws Exception { //Here we added REST Resource e.jersey().register(new EmployeeRESTController(e.getValidator())); //Now we added REST Client Resource named RESTClientController final HttpClient client = new HttpClientBuilder(e).build("DemoRESTClient"); e.jersey().register(new RESTClientController(client)); } ``` `HttpClientConfiguration`的默認配置如下: ```java timeout: 500ms connectionTimeout: 500ms timeToLive: 1 hour cookiesEnabled: false maxConnections: 1024 maxConnectionsPerRoute: 1024 keepAlive: 0s ``` `JerseyClientConfiguration`的默認配置如下: ```java minThreads: 1 maxThreads: 128 gzipEnabled: true gzipEnabledForRequests: true //same as HttpClientConfiguration timeout: 500ms connectionTimeout: 500ms timeToLive: 1 hour cookiesEnabled: false maxConnections: 1024 maxConnectionsPerRoute: 1024 keepAlive: 0s ``` ## Dropwizard REST 客戶端資源 現在,當您可以訪問 REST 客戶端資源`RESTClientController.java`中的`javax.ws.rs.client.Client`或`org.apache.http.client.HttpClient`時,您可以使用庫方法來照常調用 HTTP URI。 ```java package com.howtodoinjava.rest.controller; import java.util.ArrayList; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.client.Client; import javax.ws.rs.client.Invocation; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.howtodoinjava.rest.representations.Employee; @Produces(MediaType.TEXT_PLAIN) @Path("/client/") public class RESTClientController { private Client client; public RESTClientController(Client client) { this.client = client; } @GET @Path("/employees/") public String getEmployees() { //Do not hard code in your application WebTarget webTarget = client.target("http://localhost:8080/employees"); Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); Response response = invocationBuilder.get(); @SuppressWarnings("rawtypes") ArrayList employees = response.readEntity(ArrayList.class); return employees.toString(); } @GET @Path("/employees/{id}") public String getEmployeeById(@PathParam("id") int id) { //Do not hard code in your application WebTarget webTarget = client.target("http://localhost:8080/employees/"+id); Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); Response response = invocationBuilder.get(); Employee employee = response.readEntity(Employee.class); return employee.toString(); } } ``` 在上面的類中,我訪問了在 [dropwizard HelloWorld 教程](//howtodoinjava.com/dropwizard/tutorial-and-hello-world-example/)中創建的 REST API。 訪問 API 之后,我以純文本形式返回了響應,如下圖所示。 ![Drop Wizard REST Client](https://img.kancloud.cn/72/a7/72a749c551e2d966d950c42817c21797_949x369.png) DropWizard REST 客戶端 我已將客戶端資源類的上下文路徑設置為`/client/`,以在邏輯上分離客戶端端點和服務端點的 URI。 > 閱讀更多 : > > [Jersey RESTful 客戶端示例](//howtodoinjava.com/jersey/jersey-restful-client-examples/) > > [Apache HttpClient GET/POST 請求示例](//howtodoinjava.com/apache-commons/jax-rs-restful-client-using-apache-httpclient/) [源碼下載](//howtodoinjava.com/wp-content/downloads/DropWizardExample.zip) 將我的問題放在評論部分。 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看