原文: https://howtodoinjava.com/resteasy/jax-rs-2-0-resteasy-3-0-2-final-client-api-example/

JAX-RS 2.0 在以前的版本中带来了很多改进。 主要改进之一是客户端 API,它在 JAX-RS 1.0 中完全丢失。 尽管编写可移植的 JAX-RS 服务很容易,但是每个 JAX-RS 实现都定义了自己的专有 API。 JAX-RS 2.0 用流畅的,低级的请求构建 API 填补了这一空白。 这是一个简单的示例:

  1. Client client = ClientFactory.newClient();
  2. WebTarget target = client.target("http://localhost:8080/howtodoinjava");
  3. Form form = new Form().param("customer", "Bill").param("product", "book");
  4. Response response = target.request().post(Entity.form(form));
  5. Order order = response.readEntity(Order.class);

上面的代码特定于 JAX-RS 2.0,并使用 JAX-RS 类。 如果您正在使用最新的 RESTEasy (版本 3)内部版本,则可以使用其客户端 API 提供的 RESTEasy 抽象的这些较低级别的 JAX-RS 2.0 API。

JAX-RS RESTEasy API

让我们以示例网络服务 API 为例,我们将在客户端代码中对其进行访问:

  1. @GET
  2. @Path("/users")
  3. @Produces("application/vnd.com.demo.user-management.users+xml;charset=UTF-8;version=1")
  4. public Users getAllUsers() {
  5. User user1 = new User();
  6. user1.setId(1);
  7. user1.setFirstName("demo");
  8. user1.setLastName("user");
  9. user1.setUri("/user-management/users/1");
  10. User user2 = new User();
  11. user2.setId(2);
  12. user2.setFirstName("Mark");
  13. user2.setLastName("Dwain");
  14. user2.setUri("/user-management/users/2");
  15. Users users = new Users();
  16. users.setUsers(new ArrayList<User>());
  17. users.getUsers().add(user1);
  18. users.getUsers().add(user2);
  19. return users;
  20. }
  21. @POST
  22. @Path("/users")
  23. @Consumes("application/vnd.com.demo.user-management.user+xml;charset=UTF-8;version=1")
  24. public Response createUser(User user,
  25. @DefaultValue("false") @QueryParam("allow-admin") boolean allowAdmin)
  26. throws URISyntaxException {
  27. System.out.println(user.getFirstName());
  28. System.out.println(user.getLastName());
  29. return Response.status(201)
  30. .contentLocation(new URI("/user-management/users/123")).build();
  31. }

客户端代码

现在,使用新客户端代码访问这些 API:

  1. package test.jaxrs2;
  2. import javax.ws.rs.client.Entity;
  3. import javax.ws.rs.core.Response;
  4. import org.jboss.resteasy.client.jaxrs.ResteasyClient;
  5. import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
  6. import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
  7. import com.demo.rest.model.User;
  8. import com.demo.rest.model.Users;
  9. public class Demo_JAXRS_2_Example
  10. {
  11. public static void main(String[] args)
  12. {
  13. getExample_one();
  14. getExample_two();
  15. postExample();
  16. }
  17. private static void getExample_one()
  18. {
  19. ResteasyClient client = new ResteasyClientBuilder().build();
  20. ResteasyWebTarget target = client.target("http://localhost:8080/RESTEasyApplication/user-management/users");
  21. Response response = target.request().get();
  22. //Read output in string format
  23. String value = response.readEntity(String.class);
  24. System.out.println(value);
  25. response.close();
  26. }
  27. private static void getExample_two()
  28. {
  29. ResteasyClient client = new ResteasyClientBuilder().build();
  30. ResteasyWebTarget target = client.target("http://localhost:8080/RESTEasyApplication/user-management/users");
  31. Response response = target.request().get();
  32. //Read the entity
  33. Users users = response.readEntity(Users.class);
  34. for(User user : users.getUsers()){
  35. System.out.println(user.getId());
  36. System.out.println(user.getLastName());
  37. }
  38. response.close();
  39. }
  40. private static void postExample()
  41. {
  42. User user = new User();
  43. user.setFirstName("john");
  44. user.setLastName("Maclane");
  45. ResteasyClient client = new ResteasyClientBuilder().build();
  46. ResteasyWebTarget target = client.target("http://localhost:8080/RESTEasyApplication/user-management/users");
  47. Response response = target.request().post(Entity.entity(user, "application/vnd.com.demo.user-management.user+xml;charset=UTF-8;version=1"));
  48. //Read output in string format
  49. System.out.println(response.getStatus());
  50. response.close();
  51. }
  52. }
  53. Output in console:
  54. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  55. <users>
  56. <user id="1" uri="/user-management/users/1"><firstName>demo</firstName><lastName>user</lastName></user>
  57. <user id="2" uri="/user-management/users/2"><firstName>demo</firstName><lastName>user</lastName></user>
  58. </users>
  59. 1
  60. user
  61. 2
  62. Dwain
  63. 201

Maven 配置

我已经使用下面的 Maven 配置来运行这些示例。

  1. <repositories>
  2. <repository>
  3. <id>jboss</id>
  4. <url>http://repository.jboss.org/maven2</url>
  5. </repository>
  6. </repositories>
  7. <dependencies>
  8. <dependency>
  9. <groupId>junit</groupId>
  10. <artifactId>junit</artifactId>
  11. <version>4.11</version>
  12. <scope>test</scope>
  13. </dependency>
  14. <!-- core library -->
  15. <dependency>
  16. <groupId>org.jboss.resteasy</groupId>
  17. <artifactId>resteasy-jaxrs</artifactId>
  18. <version>3.0.2.Final</version>
  19. </dependency>
  20. <!-- JAXB support -->
  21. <dependency>
  22. <groupId>org.jboss.resteasy</groupId>
  23. <artifactId>resteasy-jaxb-provider</artifactId>
  24. <version>3.0.2.Final</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.jboss.resteasy</groupId>
  28. <artifactId>jaxrs-api</artifactId>
  29. <version>3.0.2.Final</version>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.jboss.resteasy</groupId>
  33. <artifactId>resteasy-client</artifactId>
  34. <version>3.0.2.Final</version>
  35. </dependency>
  36. <dependency>
  37. <groupId>net.sf.scannotation</groupId>
  38. <artifactId>scannotation</artifactId>
  39. <version>1.0.3</version>
  40. </dependency>
  41. </dependencies>

要下载以上示例的源代码,请点击以下链接。

  1. [下载源码](https://docs.google.com/file/d/0B7yo2HclmjI4NS1IQUZLUjI1Q0U/edit?usp=sharing "jax-rs 2.0 resteasy client code swource code")

学习愉快!