<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國際加速解決方案。 廣告
                # 使用 RESTful API 的 RESTEasy 客戶端 > 原文: [https://howtodoinjava.com/resteasy/resteasy-client-for-consuming-rest-apis/](https://howtodoinjava.com/resteasy/resteasy-client-for-consuming-rest-apis/) 到目前為止,在此博客中,我們已經學習了有關構建服務器端組件 [**RESTful Web 服務**](//howtodoinjava.com/restful-web-service/ "restful-web-service")的知識。 在本文中,我們將學習構建一個 RESTful 客戶端,以使用先前文章中編寫的 Web 服務。 我將重新使用為 [RESTEasy + JAXB xml 示例](//howtodoinjava.com/resteasy/resteasy-jaxb-xml-example/ "RESTEasy + JAXB xml example")編寫的代碼庫。 我將要訪問的 API 已定義。 ```java @GET @Path("/users/{id}") public User getUserById (@PathParam("id") Integer id) { User user = new User(); user.setId(id); user.setFirstName("demo"); user.setLastName("user"); return user; } @POST @Path("/users") public User addUser() { //Some code } ``` 要使用 JAX-RS RESTEasy 的客戶端功能構建 RESTful 客戶端,請遵循給定的說明。 **1)驗證以下 RESTEasy 依賴項** ```java <!-- core library --> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxrs</artifactId> <version>2.3.1.GA</version> </dependency> <dependency> <groupId>net.sf.scannotation</groupId> <artifactId>scannotation</artifactId> <version>1.0.2</version> </dependency> <!-- JAXB provider --> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxb-provider</artifactId> <version>2.3.1.GA</version> </dependency> ``` **2)編寫客戶端代碼以訪問 GET API,如下所示** ```java public static void sampleResteasyClientGETRequest() throws Exception { //Define the API URI where API will be accessed ClientRequest request = new ClientRequest("http://localhost:8080/RESTfulDemoApplication/user-management/users/10"); //Set the accept header to tell the accepted response format request.accept("application/xml"); //RESTEasy client automatically converts the response to desired objects. //This is how it is done. //Populate the response in user object ClientResponse<User> response = request.get(User.class); //First validate the api status code int apiResponseCode = response.getResponseStatus().getStatusCode(); if(response.getResponseStatus().getStatusCode() != 200) { throw new RuntimeException("Failed with HTTP error code : " + apiResponseCode); } //Get the user object from entity User user = response.getEntity(); //verify the user object System.out.println(user.getId()); System.out.println(user.getFirstName()); System.out.println(user.getLastName()); } ``` 3)編寫客戶端代碼以訪問 POST API,如下所示 ```java public static void sampleResteasyClientPostRequest() throws Exception { User user = new User(); user.setId(100); user.setFirstName("Lokesh"); user.setLastName("Gupta"); StringWriter writer = new StringWriter(); JAXBContext jaxbContext = JAXBContext.newInstance(User.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.marshal(user, writer); //Define the API URI where API will be accessed ClientRequest request = new ClientRequest("http://localhost:8080/RESTfulDemoApplication/user-management/users"); //Set the accept header to tell the accepted response format request.body("application/xml", writer.getBuffer().toString()); //Send the request ClientResponse response = request.post(); //First validate the api status code int apiResponseCode = response.getResponseStatus().getStatusCode(); if(response.getResponseStatus().getStatusCode() != 201) { throw new RuntimeException("Failed with HTTP error code : " + apiResponseCode); } } ``` **祝您學習愉快!**
                  <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>

                              哎呀哎呀视频在线观看