3 Developing RESTful Web Service Clients

You can develop Java EE web service clients that conform to the Representational State Transfer (REST) architectural style using the Jersey 2.x Java API for RESTful Web Services (JAX-RS) 2.1 reference implementation (RI).

Note:

Support for the Jersey 1.18 (JAX-RS 1.1RI) client APIs are deprecated in this release of WebLogic Server but are maintained for backward compatibility. See Develop RESTful Web Service Clients Using Jersey 1.18 (JAX-RS 1.1 RI)

Oracle recommends that you update your RESTful client applications to use the Jersey 2.x (JAX-RS 2.1 RI) client APIs as described in this chapter at your earliest convenience.

This chapter includes the following sections:

Summary of Tasks to Develop RESTful Web Service Clients

Some of the tasks required to develop a RESTful web service client include creating the client class, targeting a web resource, identifying resources on the target, and more. The following table summarizes a subset of the tasks that are required to develop RESTful web service clients using Jersey 2.x (JAX-RS 2.0 RI).

Table 3-1 Summary of Tasks to Develop RESTful Web Service Clients

Task More Information

Create and configure an instance of the javax.ws.rs.client.Client class.

Creating and configuring a Client instance in Jersey 2.29 User Guide

Target the Web resource.

Targeting a web resource in Jersey 2.29 User Guide

Identify resources on WebTarget.

Identifying resource on WebTarget in Jersey 2.29 User Guide

Invoke an HTTP request.

Invoking a HTTP request in Jersey 2.29 User Guide

For information about developing RESTful web service clients using Oracle JDeveloper, see Creating RESTful Web Services and Clients in Developing Applications with Oracle JDeveloper.

Example of a RESTful Web Service Client

You can learn more about how to create a RESTful web service client by viewing an example.The following is a simple example that shows how a client can be used to call the RESTful web service defined in Example 2-1. In this example:
  • The Client instance is created and a WebTarget defined.

  • The resource path is defined to access the Web resource.

  • The Invocation.Builder is used to send a get request to the resource.

  • The response is returned as a String value.

Example 3-1 Simple RESTful Web Service Client Using Jersey 2.x (JAX-RS 2.0 RI)

package samples.helloworld.client;
...
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
 
public class helloWorldClient{
    public static void main(String[] args) {
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target("http://localhost:7101/restservice");
        WebTarget resourceWebTarget;
        resourceWebTarget = target.path("resources/helloworld");
        Invocation.Builder invocationBuilder;
        invocationBuilder = resourceWebTarget.request(
          MediaType.TEXT_PLAIN_TYPE);
        Response response = invocationBuilder.get();
        System.out.println(response.getStatus());
        System.out.println(response.readEntity(String.class));
...
    }
...
}

For complete details, see Client API in Jersey 2.29 User Guide.

Invoking a RESTful Web Service from a Standalone Client

When invoking a RESTful web service from an environment that does not have Oracle Fusion Middleware or WebLogic Server installed locally, and without the entire set of Oracle Fusion Middleware or WebLogic Server classes in the CLASSPATH, you can use the standalone client JAR file when invoking the web service.

The standalone RESTful web service client JAR supports basic JAX-RS client-side functionality and OWSM security policies.

To use the standalone RESTful web service client JAR file with your client application, perform the following steps:

  1. Create a Java SE client using your favorite IDE, such as Oracle JDeveloper. See Developing and Securing Web Services in Developing Applications with Oracle JDeveloper.

  2. Copy the file ORACLE_HOME/oracle_common/modules/clients/com.oracle.jersey.fmw.client.jar from the computer hosting Oracle Fusion Middleware to the client computer, where ORACLE_HOME is the directory you specified as Oracle Home when you installed Oracle Fusion Middleware.

    For example, you might copy the file into the directory that contains other classes used by your client application.

  3. Add the JAR file to your CLASSPATH.

    Note:

    Ensure that your CLASSPATH includes the JAR file that contains the Ant classes (ant.jar) as a subset are used by the standalone client JAR files. This JAR file is typically located in the lib directory of the Ant distribution.

Using the Reactive JAX-RS Client API

Reactive Client API is part of the JAX-RS 2.1 specification.

All invocations in the client API are set in synchronous mode by default. In synchronous processing, each request is processed in a single HTTP thread. After the processing is finished, the thread is returned back to the pool. This approach can result in taking more time to complete and unnecessary blocking of the resources.

Asynchronous programming in JAX-RS enables client to unblock certain threads by pushing the work to background threads which can be monitored and joined at a later time. The resources are used optimally to achieve quick response time.

In JAX-RS 2.1, you can achieve asynchronous programming by providing an instance of InvocationCallback, which also enables a more reactive programming style in which the user-provided code reacts only when a certain event has occurred. Callback works well for simple cases but the coding becomes complex when multiple events come into play. To make the asynchronous programming more readable, a new interface CompletionStage is introduced for managing large number of methods dedicated for asynchronous computations.

See Usage and Extension Modules in Jersey 2.29 User Guide for more information about the different types of invokers based on CompletionStage.

See Reactive JAX-RS Client API in Jersey 2.29 User Guide for more detailed information.

Note:

In WebLogic Server, the following reactive libraries are not supported:
  • RxJava (Observable)
  • RxJava (Flowable)
  • Guava (ListenableFuture)