Liberty Repository[8.5.5.6 or later]
This topic applies to WebSphere Application Server Liberty V8.5.5.9 and earlier. For the latest Liberty topics, see the WebSphere Application Server Liberty documentation.

Configuring JAX-RS 2.0 client

For Java API for XML RESTful Web Services 2.0, you can configure the client to access REST endpoints. JAX-RS 2.0 introduces a new and standardized Client API so that you can make HTTP requests to your remote RESTful web services.

About this task

An instance of Client is required to access a Web resource using the Client API. The default instance of Client can be obtained by calling newClient or build on ClientBuilder.

Procedure

  1. Enable the jaxrsClient-2.0 or jaxrs-2.0 feature in your server.xml file:
    <featureManager>
        <feature>jaxrs-2.0</feature>// If you only need the JAX-RS 2.0 client feature, you can enable jaxrsClient-2.0 instead of  jaxrs-2.0 
    </featureManager>
  2. Create a JAX-RS 2.0 client and send the request to the server:
    javax.ws.rs.client.ClientBuilder cb = ClientBuilder.newBuilder();
    
    javax.ws.rs.client.Client c = cb.build();
    String res = null;
    
    try {
    	res = c.target("<Resource_URL>")
                 .path("<PATH>")
                 .request()
                 .get(String.class);
    	} catch (Exception e) {
          	res = "[Error]:" + e.toString();
          } finally {
                c.close();        
          }   
    For more information about the asynchronous JAX-RS 2.0 client, see Asynchronous processing.

What to do next

    • Liberty Repository[8.5.5.6 or later]Use the com.ibm.ws.jaxrs.client.timeout client property to set the timeout value.
      javax.ws.rs.client.ClientBuilder cb = ClientBuilder.newBuilder();
               cb.property("com.ibm.ws.jaxrs.client.timeout", "1000"); 
               Client c = cb.build(); 
      Tip: The value of the timeout property is milliseconds, and the type must be long or int. If the type of the value is invalid, the following message is displayed:
      CWWKW0700E: The timeout value {0} that you specified in the property com.ibm.ws.jaxrs.client.timeout on the JAX-RS Client side is invalid. The value is set to default 30000.{3}
    • Liberty Repository[8.5.5.7 or later]Use the com.ibm.ws.jaxrs.client.connection.timeout client property and the com.ibm.ws.jaxrs.client.receive.timeout client property to set the timeout values.
      • com.ibm.ws.jaxrs.client.connection.timeout
        javax.ws.rs.client.ClientBuilder cb = ClientBuilder.newBuilder();
                cb.property("com.ibm.ws.jaxrs.client.connection.timeout", "1000"); 
                Client c = cb.build();
      • com.ibm.ws.jaxrs.client.receive.timeout
        javax.ws.rs.client.ClientBuilder cb = ClientBuilder.newBuilder();
                cb.property("com.ibm.ws.jaxrs.client.receive.timeout", "1000"); 
                Client c = cb.build();
      Tip: The value of the timeout property is millisecond, and the type must be long or int. If the type of the value is invalid, the following message is displayed:
      CWWKW0700E: The timeout value {0} that you specified in the property com.ibm.ws.jaxrs.client.receive.timeout on the JAX-RS Client side is invalid. The value is set to default 30000. {3}
  • Use the following client properties for client proxy support:
    ClientBuilder cb = ClientBuilder.newBuilder();
    cb.property("com.ibm.ws.jaxrs.client.proxy.host", "hostname");
    cb.property("com.ibm.ws.jaxrs.client.proxy.port", "8888";);
    cb.property("com.ibm.ws.jaxrs.client.proxy.type", "HTTP");
    
    Client c = cb.build();  
    • com.ibm.ws.jaxrs.client.proxy.host
    • com.ibm.ws.jaxrs.client.proxy.port
      Tip: The type of the proxy server port value must be int. The default value is 80. If the value type is invalid, the following message is displayed:
      CWWKW0701E: The proxy server port value {0} that you specified in the property com.ibm.ws.jaxrs.client.proxy.port on the JAX-RS Client side is invalid. The value is set to default 80. {3}
    • com.ibm.ws.jaxrs.client.proxy.type
      Tip: The value of the proxy server type must be HTTP or SOCKS. The default value is HTTP. If the type of the proxy server is invalid, the following message is displayed:
      CWWKW0702E: The proxy server type value {0} that you specified in the property com.ibm.ws.jaxrs.client.proxy.type on the JAX-RS Client side is invalid. The value is set to default HTTP. {3}
  • Use the com.ibm.ws.jaxrs.client.ltpa.handler client property to set the SSO cookie and set the value to true.
    ClientBuilder cb = ClientBuilder.newBuilder();
            Client c = cb.build();
            c.property("com.ibm.ws.jaxrs.client.ltpa.handler", "true");
    If you want to use the Secure Sockets Layer (SSL) function in JAX-RS 2.0, you need to enable the ssl-1.0 or appSecurity-2.0 feature. For the LTPA token function, you must enable the appSecurity-2.0 feature.

    For more information about how to configure the environment to run the JAX-RS 2.0 client with SSL through IHS, see Configuring IBM HTTP server SSL support.

    Note: The ssl-1.0 feature is a subfeature of the appSecurity-2.0 feature. If you enable the jaxrsClient-2.0 feature and the ssl-1.0 feature, the appSecurity-2.0 feature is enabled automatically.
  • Use the com.ibm.ws.jaxrs.client.ssl.config client property to set the SSL reference id of your server.xml file.
    ClientBuilder cb = ClientBuilder.newBuilder();
            cb.property("com.ibm.ws.jaxrs.client.ssl.config", "mySSLRefId"); 
            Client c = cb.build();
    For more information about establishing trust by extracting the certificate from the IHS key file and adding it to the Liberty JKS file, see Create a key database file and certificates needed to authenticate the Web server during an SSL handshake.
    Note: The configuration in the server.xml file shows as follows:
    <ssl id="mySSLRefId" keyStoreRef="clientKeyStore" trustStoreRef="clientTrustStore" />
  • [8.5.5.9 or later]Use the com.ibm.ws.jaxrs.client.disableCNCheck client property to disable the common name check.
    ClientBuilder cb = ClientBuilder.newBuilder();
    cb.property("com.ibm.ws.jaxrs.client.disableCNCheck", true);

Icon that indicates the type of topic Task topic



Timestamp icon Last updated: Tuesday, 12 December 2017
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=phil&product=was-libcore-mp&topic=twlp_jaxrs2.0_clientconfig
File name: twlp_jaxrs2.0_clientconfig.html