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.0 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.0 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
Table 3-1 Summary of Tasks to Develop RESTful Web Service Clients
Task | More Information |
---|---|
Create and configure an instance of the |
Creating and configuring a Client instance in Jersey 2.22 User Guide |
Target the Web resource. |
Targeting a web resource in Jersey 2.22 User Guide |
Identify resources on WebTarget. |
Identifying resource on WebTarget in Jersey 2.22 User Guide |
Invoke an HTTP request. |
Invoking a HTTP request in Jersey 2.22 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
-
The
Client
instance is created and aWebTarget
defined. -
The resource path is defined to access the Web resource.
-
The
Invocation.Builder
is used to send aget
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.22 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:
-
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.
-
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, whereORACLE_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.
-
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 thelib
directory of the Ant distribution. -
Configure your environment for Oracle Web Services Manager (OWSM) policies. This step is optional, required only if you are attaching OWSM security policies to the RESTful web services client.
The configuration steps required vary based on the type of policy being attached. Examples are provided below. For additional configuration requirements, see Configuring Java SE Applications to Use OPSS in Securing Applications with Oracle Platform Security Services.
Example: Basic Authentication
For example, to support basic authentication using the
oracle/wss_http_token_client_policy
security policy, perform the following steps:-
Copy the
jps-config-jse.xml
andaudit-store.xml
files from thedomain_home
/config/fmwconfig
directory, wheredomain_home
is the name and location of the domain, to a location that is accessible to the RESTful client. -
Create a wallet (
cwallet.sso
) in the same location that you copied the files in step 2 that defines a map calledoracle.wsm.security
and the credential key name that the client application will use (for example,weblogic-csf-key
).The location of the file
cwallet.sso
is specified in the configuration filejps-config-jse.xml
with the element<serviceInstance>
. See Using File Credential Stores in Securing Applications with Oracle Platform Security Services. -
On the Java command line, pass the following property defining the JPS configuration file copied in step 1:
-Doracle.security.jps.config=<pathToConfigFile>
See About Java SE Application Security in Securing Applications with Oracle Platform Security Services.
Example: SSL
For example, to support SSL policies, perform the following steps:
-
Copy the
jps-config-jse.xml
andaudit-store.xml
files from thedomain_home
/config/fmwconfig
directory, wheredomain_home
is the name and location of the domain, to a location that is accessible to the RESTful client. -
On the Java command line, pass the following properties defining the JPS configuration file copied in step 1:
Define the JPS configuration file copied in step 1:
-Doracle.security.jps.config=<pathToConfigFile>
See About Java SE Application Security in Securing Applications with Oracle Platform Security Services.
Define the trust store containing the trusted certificates:
-Djavax.net.ssl.trustStore=<trustStore>
See Setting Up One-Way SSL to the LDAP Security Store in Administering Oracle Fusion Middleware.
Define the trust store password:
-Djavax.net.ssl.trustStorePassword=<password>
-