RESTEasy Client for Consuming RESTful APIs

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

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

11 thoughts on “RESTEasy Client for Consuming RESTful APIs”

  1. Hi Lokesh – Could you please update this tutorial or create new one for RestEasy 3.0.x ? Certainly most of the APIs of RestEasy are deprecated, so expecting the code with new API. Please do the needful

    Reply
  2. Hi Lokesh ,

    Plz help me in my requirement. A j2se application is running in windows server 2008 and in that application I have to do modification to communicate with Rest provider API.can I use RestClient to consume that RestFul Provider API in a my J2SE application. If yes, how to do that.

    Reply
  3. Hi Lokesh,

    I want to get information on server-side about the client from where the request was executed?
    For example: I fire a request to my REST server via a Firefox REST client, then is there any way using which i can identity that this request has come from a firefox rest client?

    Can we get such information using REST-Easy?

    Please help

    Thanks in advance.

    Regards,
    AP

    Reply
  4. Hi,

    My requirement is very simple. I want to invoke an HTTP PUT (or POST) request in whose body would be an XML content. This HTTP request would be mapped to a RESTful resource on the server. Now, once the request is received on the server side, I would simply like to extract whatever XML content is there in the body of HTTP Request (PUT/POST). How can I do it?

    I am trying my RESTful component with Jersey or RestEasy implementation.

    Waiting for a reply.

    sincerely,
    Nawazish

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.