企业🤖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") ``` 学习愉快!