Wednesday, 2 July 2014

Websphere Commerce Rest Service Part 2 : Make Rest Service Calls from java class

Read the post on Websphere Commerce Rest Service Part 1 till WCS7 Feature pack 6 before proceeding with the remaining section of the page.

REST services overview

Lighter weight than SOAP-based web services. Easily adopted by a variety of clients.Use existing HTTP verbs.(GET, POST, PUT, DELETE)

Provide responses in any Internet media type

High-level declarative programming model that simplifies the development of Restful Web services.Built-in support for best-practice HTTP usage patterns and conventions.
Library used :Apache Wink
Contain JAX-RS annotations such as
@Path, @Produces, @Consumes, @QueryParam, and @PathParam

Method 1)REST Service Client in  IBM WCS using  Apache Wink( Invoke Outbound  Rest Service invoked using   REST service client)

For calling a when trying to Access a rest service (Outbound  Rest Service From WCS) using   REST service client

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

By default, the client uses the for issuing requests and responses; but the Apache HttpClient can also be used.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;
import org.apache.wink.client.RestClient;

import org.apache.wink.client.ClientResponse;

import org.apache.wink.client.Resource;

ClientConfig clientConfig = new ApacheHttpClientConfig();

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

RestClient restClient = new RestClient(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.

Resource restResource = restClient.resource("");
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.

ClientResponse clientResponse =;
int statusCode = clientResponse.getStatusCode();
String responseEntity = clientRepsonse.getEntity(String.class);

Method 2)Below code shows how to create a RESTful Java client with Apache HttpClient,  to perform a “GET” request.

The code snippet below provides a method to get all the top categories  from WebSphere Commerce store with store ID 10151.

private void getCategories(){
DefaultHttpClient httpclient = new DefaultHttpClient();
 // REST URL to access top categories from commerce server's store 10151
String url="http://localhost/wcs/resources/store/10151/categoryview/@top";
try {
HttpGet httpget=new HttpGet(url);
HttpResponse clientResponse = httpclient.execute(httpget);
String responseAsString = EntityUtils.toString(clientResponse.getEntity());
JSONObject jsonResponse = new JSONObject(responseAsString);
JSONArray array = jsonResponse.getJSONArray("CatalogGroupView");
int size = array.length();
for(int i=0; i<size; i++){
String message = array.getJSONObject(i).getString("shortDescription");

Limitations when working with the WebSphere Commerce REST API.
1.Review the available REST services to ensure the functionality you are implementing is available.For example, promotion codes are not supported by default when using REST services in the CartHandlerpre-checkout and checkout flows. 
2.REST services are primarily designed to work with the Madisons starter store.REST services are not supported in starter stores that support the B2B direct business model,for example the Elite starter store. 
3.REST API handlers rely on WebSphere Commerce search to function correctly.For example, the ProductViewHandler and CategoryViewHandler use search-basedcatalog navigation.You must enable WebSphere Commerce search,or customize REST services to suit your business requirements when working with these handlers.


  1. I am not able to use the REST api from javascript.

    var settings = {
    "async": true,
    "crossDomain": true,
    "url": "http://localhost:80/wcs/resources/store/10152/associated_promotion?qCategoryId=10043&qUserId=6002&q=byCategory",
    "method": "GET",
    "headers": {
    "cache-control": "no-cache",
    "postman-token": "6d51031c-1c1b-a810-01d2-59abe9fabe17"

    $.ajax(settings).done(function(response) {

    Such a code is failing

  2. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.