<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # JAX-RS 2.0 RESTEasy 3.0.2.Final 客戶端 API 示例 > 原文: [https://howtodoinjava.com/resteasy/jax-rs-2-0-resteasy-3-0-2-final-client-api-example/](https://howtodoinjava.com/resteasy/jax-rs-2-0-resteasy-3-0-2-final-client-api-example/) [**JAX-RS 2.0**](//howtodoinjava.com/tag/jax-rs-2-0/ "jax-rs 2.0") 在以前的版本中帶來了很多改進。 主要改進之一是客戶端 API,它在 JAX-RS 1.0 中完全丟失。 盡管編寫可移植的 JAX-RS 服務很容易,但是每個 JAX-RS 實現都定義了自己的專有 API。 JAX-RS 2.0 用流暢的,低級的請求構建 API 填補了這一空白。 這是一個簡單的示例: ```java Client client = ClientFactory.newClient(); WebTarget target = client.target("http://localhost:8080/howtodoinjava"); Form form = new Form().param("customer", "Bill").param("product", "book"); Response response = target.request().post(Entity.form(form)); Order order = response.readEntity(Order.class); ``` 上面的代碼特定于 JAX-RS 2.0,并使用 JAX-RS 類。 如果您正在使用最新的 [**RESTEasy**](//howtodoinjava.com/restful-web-service/ "RESTful Web services Tutorials") (版本 3)內部版本,則可以使用其客戶端 API 提供的 RESTEasy 抽象的這些較低級別的 JAX-RS 2.0 API。 ## JAX-RS RESTEasy API 讓我們以示例網絡服務 API 為例,我們將在客戶端代碼中對其進行訪問: ```java @GET @Path("/users") @Produces("application/vnd.com.demo.user-management.users+xml;charset=UTF-8;version=1") public Users getAllUsers() { User user1 = new User(); user1.setId(1); user1.setFirstName("demo"); user1.setLastName("user"); user1.setUri("/user-management/users/1"); User user2 = new User(); user2.setId(2); user2.setFirstName("Mark"); user2.setLastName("Dwain"); user2.setUri("/user-management/users/2"); Users users = new Users(); users.setUsers(new ArrayList<User>()); users.getUsers().add(user1); users.getUsers().add(user2); return users; } @POST @Path("/users") @Consumes("application/vnd.com.demo.user-management.user+xml;charset=UTF-8;version=1") public Response createUser(User user, @DefaultValue("false") @QueryParam("allow-admin") boolean allowAdmin) throws URISyntaxException { System.out.println(user.getFirstName()); System.out.println(user.getLastName()); return Response.status(201) .contentLocation(new URI("/user-management/users/123")).build(); } ``` ## 客戶端代碼 現在,使用**新客戶端代碼**訪問這些 API: ```java package test.jaxrs2; import javax.ws.rs.client.Entity; import javax.ws.rs.core.Response; import org.jboss.resteasy.client.jaxrs.ResteasyClient; import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; import com.demo.rest.model.User; import com.demo.rest.model.Users; public class Demo_JAXRS_2_Example { public static void main(String[] args) { getExample_one(); getExample_two(); postExample(); } private static void getExample_one() { ResteasyClient client = new ResteasyClientBuilder().build(); ResteasyWebTarget target = client.target("http://localhost:8080/RESTEasyApplication/user-management/users"); Response response = target.request().get(); //Read output in string format String value = response.readEntity(String.class); System.out.println(value); response.close(); } private static void getExample_two() { ResteasyClient client = new ResteasyClientBuilder().build(); ResteasyWebTarget target = client.target("http://localhost:8080/RESTEasyApplication/user-management/users"); Response response = target.request().get(); //Read the entity Users users = response.readEntity(Users.class); for(User user : users.getUsers()){ System.out.println(user.getId()); System.out.println(user.getLastName()); } response.close(); } private static void postExample() { User user = new User(); user.setFirstName("john"); user.setLastName("Maclane"); ResteasyClient client = new ResteasyClientBuilder().build(); ResteasyWebTarget target = client.target("http://localhost:8080/RESTEasyApplication/user-management/users"); Response response = target.request().post(Entity.entity(user, "application/vnd.com.demo.user-management.user+xml;charset=UTF-8;version=1")); //Read output in string format System.out.println(response.getStatus()); response.close(); } } Output in console: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <users> <user id="1" uri="/user-management/users/1"><firstName>demo</firstName><lastName>user</lastName></user> <user id="2" uri="/user-management/users/2"><firstName>demo</firstName><lastName>user</lastName></user> </users> 1 user 2 Dwain 201 ``` ## Maven 配置 我已經使用下面的 Maven 配置來運行這些示例。 ```java <repositories> <repository> <id>jboss</id> <url>http://repository.jboss.org/maven2</url> </repository> </repositories> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- core library --> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxrs</artifactId> <version>3.0.2.Final</version> </dependency> <!-- JAXB support --> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxb-provider</artifactId> <version>3.0.2.Final</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>jaxrs-api</artifactId> <version>3.0.2.Final</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-client</artifactId> <version>3.0.2.Final</version> </dependency> <dependency> <groupId>net.sf.scannotation</groupId> <artifactId>scannotation</artifactId> <version>1.0.3</version> </dependency> </dependencies> ``` 要下載以上示例的源代碼,請點擊以下鏈接。 ```java [下載源碼](https://docs.google.com/file/d/0B7yo2HclmjI4NS1IQUZLUjI1Q0U/edit?usp=sharing "jax-rs 2.0 resteasy client code swource code") ``` 學習愉快!
                  <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>

                              哎呀哎呀视频在线观看