The Client API

The Client API is a high-level API used to access web resources and integrate with JAX-RS providers and is included in the javax.ws.rs.client package.

Note

Previously, different implementations provided the Client API, but in JAX-RS 2.0, the Client API is provided as a core API.

Creating a client instance

A Client instance is required to build and run client requests to access or consume web resources. In the RESTEasy client class RESTEasyClient.java, create a Client instance from ClientBuilder using the newClient() method as follows:

Client client = ClientBuilder.newClient();

Providers, filters, and features can be configured with the Client object using the register() method. For example, the org.jboss.resteasy.plugins.providers.JaxrsFormProvider.class provider class is registered as follows:

client.register(org.jboss.resteasy.plugins.providers.JaxrsFormProvider.class);

Accessing a resource

The Client API is used to access a web resource as follows:

  1. Create a WebTarget object from the resource URI using the overloaded target() method of the Client object. The path appended to the URI is to enable the REST service to handle multiple inputs:
    WebTarget target = client.target("http://localhost:8080/jboss-resteasy/rest/helloworld");
  2. Add one or more path elements to the WebTarget object if required using the path() method, which returns a WebTarget object:
    WebTarget target = target.path("text");
  3. Create a request from the WebTarget object using the overloaded request() method, in which you need to define the accepted response media types. Invoke the HTTP GET method for the request using the overloaded get() method to obtain an invocation response as a Response object:
    Response response=target.request("text/plain").get();
  4. Obtain the message entity input stream as a String object:
    String value = response.readEntity(String.class);
  5. The fluent API can be used to build and submit the client request and obtain a response by linking the method invocations:
    String response = client.target("http://localhost:8080/jboss-resteasy/rest/helloworld").path("text").request("text/plain").get(String.class);

The RESTEasyClient.java class is listed as follows:

package org.jboss.resteasy.rest;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;

import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.*;

import org.jboss.resteasy.client.jaxrs.ResteasyClient;

public class RESTEasyClient {
  
  public static void main(String[] args) {
    
    Client client =	ClientBuilder.newClient();
    
    String response = client.target("http://localhost:8080/jboss-resteasy/rest/helloworld").path("text").request("text/plain").get(String.class);
    
    
    System.out.println(response);
  }
  
}

Create a resource class hosted at the /helloworld URI path to test the Client API. Add a resource method at the relative URI path /text to return a Hello message from a name. The resource class HelloWorldResource is listed as follows:

package org.jboss.resteasy.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.Path;


@Path("/helloworld")
public class HelloWorldResource {
  
  @GET
  @Produces("text/plain")
  @Path("/text")
  public String getClichedMessage() {
    
    return "Hello John Smith";
  }
  
}

To test the resource class and the client, compile, package, and deploy the jboss-resteasy application to WildFly. We will add the output directory as the WildFly deployments directory to the configuration for the Maven WAR plugin, as shown here:

<plugin>
  <artifactId>maven-war-plugin</artifactId>
  <version>${version.war.plugin}</version>
  <configuration>
    <outputDirectory>C:wildfly-8.1.0.Finalstandalonedeployments</outputDirectory>
    <failOnMissingWebXml>false</failOnMissingWebXml>
  </configuration>
</plugin>

Right-click on pom.xml and select Run As | Maven install, which is shown as follows:

Accessing a resource

The jboss-resteasy application gets compiled, built, and outputted to the WildFly 8.1 deployments directory is indicated by the BUILD SUCCESS message in the Console, which is shown as follows:

Accessing a resource

Now, log in to the WildFly 8.1 Administration Console and click on Manage Deployments. The jboss-resteasy.war application should be listed as deployed, which is shown in the following screenshot:

Accessing a resource

To test the client, right-click on the RESTEasyClient.java class in Project Explorer and select Run As | Java Application, as shown here:

Accessing a resource

The client application runs to invoke the resource class and produces the output, as shown in the following Console:

Accessing a resource

Setting a query parameter

To invoke a resource method with parameters, the @QueryParam annotation can be used to bind request parameters to resource method parameters. In a variation of the resource class used in the previous subsection, add a String parameter to the resource method. Annotate the parameter declaration with @QueryParam and set its default value as DefaultValue:

@GET
@Produces("text/plain")
@Path("/text")
public String getClichedMessage(@QueryParam("name") @DefaultValue("John Smith") String name) {
  return "Hello " +name;
}

In the client class, the query parameter can be sent in the request using the queryParam() method as follows:

String response = client.target("http://localhost:8080/jboss-resteasy/rest/helloworld").path("text").queryParam("name", "John Smith").request("text/plain").get(String.class);

Alternatively, the query parameter can be included in the request URI, which is shown as follows:

String response = client.target("http://localhost:8080/jboss-resteasy/rest/helloworld/text?name=John Smith").request("text/plain").get(String.class);

Setting a template parameter

The resource URI can also be built using template parameters. In a variation of HelloWorldResource, specify a template parameter {name} in the @Path annotating the resource method. Bind the template parameter to the resource method parameter using the @PathParam annotation:

@GET
@Produces("text/plain")
@Path("/text/{name}")
public String getClichedMessage(@PathParam("name") String name) {
  return "Hello " +name;
}

In the RESTEasyClient class, include the value for the {name} template parameter in the resource URI as follows:

String response = client.target("http://localhost:8080/jboss-resteasy/rest/helloworld/text/John Smith").request("text/plain").get(String.class);

Redeploy the jboss-resteasy application and rerun the client to produce the same output.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset