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 request to 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 async JAX-RS 2.0 client, you can see Asynchronous processing.

What to do next

  • 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 value.
    • 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 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, the appSecurity-2.0 feature is must.

    For more information about how to configure the environment to have JAX-RS 2.0 client to run with SSL through IHS, please 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 server.xml.
    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 certificate from IHS key file and add it to the Liberty JKS file, please see Create a key database file and certificates needed to authenticate the Web server during an SSL handshake.
    Note: The configuration in server.xml shows as follows:
    <ssl id="mySSLRefId" keyStoreRef="clientKeyStore" trustStoreRef="clientTrustStore" />
  • 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: Monday, 5 December 2016
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=twlp_jaxrs2.0_clientconfig
File name: twlp_jaxrs2.0_clientconfig.html