Over a million developers have joined DZone.

Creating a REST Service Client in WebSphere Commerce v7.0 with Apache Wink

DZone's Guide to

Creating a REST Service Client in WebSphere Commerce v7.0 with Apache Wink

· Java Zone
Free Resource

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

In WebSphere Commerce v7.0 inbound REST services are JAX-RS REST services and are built on top of Apache Wink. The WebSphere Commerce Information Center has plenty of documentation on how to create a inbound REST service; however, when trying to create an outbound REST service client, you are left to your own research and devices as to an implementation strategy.

Knowing that WebSphere Commerce v7.0 includes the Apache Wink framework, we can leverage this to create a REST service client that is also based upon Apache Wink, and leverages a framework pattern rather than just pieces of code put together to execute HTTP calls and retrieve the responses.

NOTE: The Apache Wink version included in WebSphere Commerce v7.0 is 1.0-incubating.

The code below is an example of a GET request that requests and receives a JSON string response. The Apache Wink framework also supports POST and other applicable request types, but for the purposes of this example a GET request will be leveraged.

Apache Wink REST Client Creation Tutorial for WebSphere Commerce v7.0

Step 1. Create a ClientConfig object. This object will be used to configure and construct a RestClient.

By default, the client uses the java.net.HttpURLConnection for issuing requests and responses; but the Apache HttpClient can also be used. In my experience with WebSphere Commerce v7.0, I recommend using the ApacheHttpClientConfig object as shown below to construct your ClientConfig object.

Configure and set appropriate properties on the ClientConfig object, such as the readTimeout and connectTimeout properties.

NOTE: After a ClientConfig object is used to construct a RestClient object, the ClientConfig object can no longer be modified and will result in a ClientConfigException error.

// import org.apache.wink.client.ApacheHttpClientConfig;
// import org.apache.wink.client.ClientConfig;
ClientConfig clientConfig = newApacheHttpClientConfig();

Step 2. Create a RestClient with the ClientConfig object created and configured previously.

// import org.apache.wink.client.RestClient;
RestClient restClient = newRestClient(clientConfig);

Step 3. Create a Resource object with your REST service URI from the REST client object. Configure and set appropriate properties on the Resource object, and add any required headers to the request. Ensure you set the appropriate MediaType you expect to receive in response to your REST service call.

// import org.apache.wink.client.Resource;
// import javax.ws.rs.core.MediaType;
Resource restResource = restClient.resource("http://www.example.com/some/rest/service");
restResource.header("HeaderField", "HeaderValue");

Step 4. Execute a GET or POST call, and process the response by using the status code, response headers, or the response message body as needed.

// import org.apache.wink.client.ClientResponse;
ClientResponse clientResponse = restResource.get();
intstatusCode = clientResponse.getStatusCode();
String responseEntity = clientRepsonse.getEntity(String.class);

NOTE: Instead of calling the response.getEntity(String.class) to retrieve a String response, you can use any other class that has a valid javax.ws.rs.ext.MessageBodyReader object.

And there you go, you’ve implemented an Apache Wink REST service client, and can now process the response as your application requires.

For further information about creating an Apache Wink REST service client, you’ll want to reference the IBM WebSphere Application Server v7.0 Information Center, and see the article Implementing clients using the Apache Wink REST client. This article is targeted towards WebSphere Application Server, and not WebSphere Commerce, so take that into account when attempting to follow applicable tutorials.

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.


Published at DZone with permission of Drew Harvey, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}