So far in this blog, we have been learning about building RESTful webservices which are server side components. In this post, we will learn to build a RESTful client for consuming the webservices written in previous posts.
I will be re-using the code base written for RESTEasy + JAXB xml example.
The APIs which I will be accessing are as defined.
@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
}
To build a RESTful client using client capabilities of JAX-RS RESTEasy, follow given instruction.
1) Verify following RESTEasy dependencies
<!-- 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) Write the client code to access the GET API as below
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) Write the client code to access the POST API as below
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);
}
}
Source code download
Happy Learning !!
Comments