Consuming a RESTful webservice with JAX-RS Client API
In this tutorial we’ll use the JAX-RS Client API to consume the sample RESTfull web service created in the previous tutorial on Creating and deploying RESTful Web Services with JAX-RS .
The JAX-RS Client API provides a high-level API for accessing any REST resources, not just JAX-RS services. The Client API is defined in the javax.ws.rs.client
package . We’ll use it to consume the HelloResource sample of restfull webservice created in previous tutorial as follows:
-
Create and configure a new maven project ( adding jaxrs and resteasy client apis dependencies to pom file )
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion >4.0.0 </modelVersion> <groupId>net.pragmatictheories.restsamples </groupId> <artifactId>HelloRESTClient </artifactId> <version>0.0.1-SNAPSHOT </version> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <!-- jaxrs -- > <dependency> <groupId>javax.ws.rs</groupId> <artifactId>javax.ws.rs-api</artifactId> <version>2.0</version> </dependency> <!-- resteasy -- > <dependency > <groupId>org.jboss.resteasy</groupId > <artifactId>resteasy-client</artifactId > <version>3.0.2.Final</version > </dependency > </dependencies > </project >
-
Add a new HelloRestClient java class with a main included where you’ll create a new JAX-RS client, define a webTarget using the base url of the webservice and consume it as follows:
public class HelloResourceClient { public static void main(String[] args) { // create new JAX-RS client Client client = ClientBuilder.newClient(); // the base url of the service WebTarget target = client.target("http://localhost:18080/HelloREST/rest"); // consuming sayHello method WebTarget hello = target.path("hello"); // get the response from the target url Response response = hello.request().get(); // read the result as a string String result = response.readEntity(String.class); System.out.println("sayHello method returned: " + result); response.close(); /*********** Consuming a rest resource with parameters ********/ /*** identified by a url ***/ // Building the relative URL manually for the sayHelloTo method WebTarget helloTo1 = target.path("hello").path("sayHelloTo").path("p1").path("p2"); Response response1 = helloTo1.request().get(); String result1 = response1.readEntity(String.class); System.out.println("sayHelloTo with path params returned: " + result1); response1.close(); /*** accessible via a url ***/ // building the relative URL manually for the sayHelloSecond method WebTarget helloTo2 = target.path("hello").path("sayHelloToSecond").queryParam("p1", "amal").queryParam("p2", "another amal"); // get the response from the target url Response response2 = helloTo2.request().get(); // read the result as a string String result2 = response2.readEntity(String.class); // print the result to the standard output System.out.println("sayHelloTo with query params returned: " + result2); response2.close(); // close client after consuming resources client.close(); } }
That’s all folks ! This might be the easiest tutorial on what consuming a restfull webservice is ! The following video is a quick demo on how to build the maven project and consume your rest resource:
Recent Comments