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 with the Client API. The default instance of Client can be obtained by calling newClient or build on ClientBuilder.

Procedure

Create a JAX-RS 2.0 client and send a 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 processing in the Client API, see Asynchronous processing or Chapter 8 of JSR 339: JAX-RS 2.0: The Java API for RESTful Web Services (the "Specification").

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 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 integral. 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 integral. 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}
  • Configure the JAX-RS 2.0 client to validate LTPA token authentication information.
  • Secure the JAX-RS 2.0 client by using SSL.
  • 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: March 5, 2017 17:29
File name: twbs_jaxrs2.0_client.html