使用 RESTful API 的 RESTEasy 客户端
原文: https://howtodoinjava.com/resteasy/resteasy-client-for-consuming-rest-apis/
到目前为止,在此博客中,我们已经学习了有关构建服务器端组件 RESTful Web 服务的知识。 在本文中,我们将学习构建一个 RESTful 客户端,以使用先前文章中编写的 Web 服务。
我将重新使用为 RESTEasy + JAXB xml 示例编写的代码库。
我将要访问的 API 已定义。
@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 依赖项
<!-- 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,如下所示
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,如下所示
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);
}
}
祝您学习愉快!