25 Building Your First Coherence REST Application

Build and run a simple Coherence REST application that accesses and uses a Coherence cache.

The Coherence examples that ship with the distribution also include an end-to-end example of a REST application. See Coherence REST Examples in Installing Oracle Coherence.

This chapter includes the following sections:

25.1 Overview of the Basic Coherence REST Example

The Coherence REST example is organized into a set of steps that are used to configure and run a basic Coherence REST application. The steps demonstrate fundamental concepts, such as: configuring a proxy server responsible for handling HTTP request, configuring a remote cache, and using the Coherence REST API.

The example in this chapter uses an embedded HTTP server in order to deploy a standalone application that does not require an application server. Additional deployment options are available. See Deploying Coherence REST.

Coherence for Java must be installed to complete the steps in this chapter. In addition, the following user-defined variables are used in this example:

  • DEV_ROOT - The path to root folder where user is performing all of the listed steps, or in other words all of the following folders are relative to DEV_ROOT.

  • COHERENCE_HOME - The path to folder containing Coherence JARs (coherence.jar and coherence-rest.jar)

25.2 Step 1: Configure the Cluster Side

Coherence REST requires both a cache and a proxy scheme. The proxy scheme must define an HTTP acceptor to handle an incoming HTTP request.

The cluster-side cache configuration deployment descriptor configures a cache and proxy. For this example, the proxy is configured to accept client HTTP requests on localhost and port 8080. A distributed cache named dist-http-example is defined and is used to store client data in the cluster.

To configure the cluster side:

  1. Create an XML file named example-server-config.xml in the DEV_ROOT\config folder.
  2. Copy the following XML to the file:
    <?xml version="1.0"?>
    <cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. Save and close the file.

25.3 Step 2: Create a User Type

Create the Person user type, which is stored in the cache and used to demonstrate basic REST operations.

To create the Person object:

  1. Create a text file in a DEV_ROOT\example folder.
  2. Copy the following Java code to the file:
    package example;
    import java.io.Serializable;
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlRootElement;
    public class Person implements Serializable {
        public Person() {}
        public Person(String name, int age)
            m_name = name;
            m_age  = age;
        public String getName() { return m_name; }
        public void setName(String name) { m_name = name; }
        public int getAge() { return m_age; }
        public void setAge(int age) {  m_age = age; }
        protected String m_name;
        protected int    m_age;
  3. Save the file as Person.java and close the file.
  4. Compile Person.java:
    javac example\Person.java

25.4 Step 3: Configure REST Services

The Coherence REST services require metadata about the cache that it exposes. The metadata includes the cache entry's key and value types as well as key converters and value marshallers. The key and value types are required in order for Coherence to be able to use built-in converters and marshallers (XML and JSON supported).

To configure the REST services:

  1. Create an XML file named coherence-rest-config.xml in DEV_ROOT\config folder.
  2. Copy the following XML to the file:
    <?xml version="1.0"?>
    <rest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"


    The <key-class> and <value-class> element can either be defined within the <resource> element or within the <cache-mapping> element in the cache configuration file.

  3. Save and close the file

25.5 Step 4: Start the Cache Server Process

REST services are exposed as part of a cache server process (DefaultCacheServer). The cache server's classpath must be configured to find all the configuration files that were created in the previous steps as well as the Person.class. The classpath must also contain the required dependency libraries. See Dependencies for Coherence REST. For the sake of brevity, all of the dependencies are placed in DEV_ROOT\libs folder and are not individually listed.

The DEV_ROOT folder should appear as follows:


The following command line starts a cache server process and explicitly names the cache configuration file created in Step 1 by using the coherence.cacheconfig system property. In addition it sets all the needed libraries and configuration files (replace dependencies with all the required library dependencies):

java -cp DEV_ROOT\config;DEV_ROOT;DEV_ROOT\libs\dependencies; 
COHERENCE_HOME\coherence-rest.jar -Dcoherence.clusterport=8090 

An example script for UNIX-based system follows:


export CLASSPATH=${DEV_ROOT}/config:${DEV_ROOT}:

java -cp ${CLASSPATH} -Dcoherence.clusterport=8090 
-Dcoherence.ttl=0 -Dcoherence.cacheconfig=
${DEV_ROOT}/config/example-server-config.xml com.tangosol.net.DefaultCacheServer

Check the console output to verify that the proxy service has started. The output message should include the following:

(thread=Proxy:ExtendHttpProxyService:HttpAcceptor, member=1): Started: HttpAcceptor{Name=Proxy:ExtendHttpProxyService:HttpAcceptor, State=(SERVICE_STARTED), HttpServer=com.tangosol.coherence.rest.server.DefaultHttpServer, LocalAddress=localhost, LocalPort=8080, ResourceConfig=com.tangosol.coherence.rest.server.DefaultResourceConfig, RootResource=com.tangosol.coherence.rest.DefaultRootResource}

25.6 Step 5: Access REST Services From a Client

Client applications use Coherence REST services to perform cache operations. There are many application platforms that provide client libraries to build HTTP-based clients. For example, the Jersey project provides Java support for client-side communication with HTTP-based REST Web services.

The following sections demonstrate the semantics for PUT, GET, and Post operations that a client would use to access the dist-http-example cache. An example Java client built using Jersey follows and requires the Jersey-client-2.12.jar library. See Performing Grid Operations with REST.

Put Operations

PUT http://localhost:8080/dist-http-example/1 
Request Body: {"name":"chris","age":30}
PUT http://localhost:8080/dist-http-example/2 
Request Body: {"name":"adam","age":26}

GET Operations

GET http://localhost:8080/dist-http-example/1.json

GET http://localhost:8080/dist-http-example/1.xml

GET http://localhost:8080/dist-http-example?q=name is 'adam'

GET http://localhost:8080/dist-http-example;p=name

GET http://localhost:8080/dist-http-example/count()

GET http://localhost:8080/dist-http-example/double-average(age)

Post Operation

POST http://localhost:8080/dist-http-example/increment(age,1)

Sample Jersey REST Client

package example;

import java.io.IOException;

import java.net.MalformedURLException;

import java.net.URISyntaxException;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

public class RestExample {
    public static void PUT(String url, MediaType mediaType, String data) {
        process(url, "put", mediaType, data);

    public static void GET(String url, MediaType mediaType) {
        process(url, "get", mediaType, null);

    public static void POST(String url, MediaType mediaType, String data) {
        process(url, "post", mediaType, data);

    public static void DELETE(String url, MediaType mediaType) {
        process(url, "delete", mediaType, null);

    public static void process(String sUrl, String action,
            MediaType mediaType,
            String data) {
        Client client = ClientBuilder.newClient();
        Response response = null;

        WebTarget webTarget = client.target(sUrl);
        String responseType = MediaType.APPLICATION_XML;
        if (mediaType == MediaType.APPLICATION_JSON_TYPE) {
            responseType = MediaType.APPLICATION_JSON;

        if (action.equalsIgnoreCase("get")) {
            response = webTarget.request(responseType).get();
        } else if (action.equalsIgnoreCase("post")) {
            Entity<String> person = Entity.entity(data, responseType);
            response = webTarget.request(responseType).post(person);
        } else if (action.equalsIgnoreCase("put")) {
            Entity<String> person = Entity.entity(data, responseType);
            response = webTarget.request(responseType).put(person);
        } else if (action.equalsIgnoreCase("delete")) {
            Entity<String> person = Entity.entity(data, responseType);
            response = webTarget.request(responseType).delete();

    public static void main(String[] args) throws URISyntaxException,
            MalformedURLException, IOException {
                MediaType.APPLICATION_XML_TYPE, null);