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 !!
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
Thanks for the feedback. I will update them soon.
Thanks a lot Lokesh for your immidiate response. Once again Thanks.
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.
All you need to do is to download the RESTEasy jar files, add them in project lib’s folder and start consuming the APIs like given in above examples. It will work.
Hello Lokesh , Can I use REST client for sending a synchronous request meaning my request will wait until it gets the response from service? If yes, How to do that or which web service will help me ?
And one more thing is, which web service I want to access in my java application, that is developed using CGI.
By default, requests are synchronous only. REST is fine for all your needs.
ok..thanks a lot…..and I want to say one more thing is,really it a very nice site and my favourite site. This site helps me a lot during my interviews….so,lot of thanks….
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
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