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:
Person
user type, which is stored in the cache and used to demonstrate basic REST operations.DefaultCacheServer
).Parent topic: Using Coherence REST
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
)
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:
Create the Person
user type, which is stored in the cache and used to demonstrate basic REST operations.
To create the Person object:
To configure the REST services:
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:
\ \config \config\example-server-config.xml \config\coherence-rest-config.xml \example \example\Person.class \libs \libs\*
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
-Dcoherence.ttl=0
-Dcoherence.cacheconfig=DEV_ROOT\config\example-server-config.xml
com.tangosol.net.DefaultCacheServer
An example script for UNIX-based system follows:
#!/bin/bash
export CLASSPATH=${DEV_ROOT}/config:${DEV_ROOT}:
${DEVROOT}/lib/dependencies:${COHERENCE_HOME}/lib/coherence.jar:
${COHERENCE_HOME}/lib/coherence-rest.jar
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}
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 Content-Type=application/json Request Body: {"name":"chris","age":30}
PUT http://localhost:8080/dist-http-example/2 Content-Type=application/json 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(); } System.out.println(response.readEntity(String.class)); } public static void main(String[] args) throws URISyntaxException, MalformedURLException, IOException { PUT("http://localhost:8080/dist-http-example/1", MediaType.APPLICATION_JSON_TYPE, "{\"name\":\"chris\",\"age\":32}"); PUT("http://localhost:8080/dist-http-example/2", MediaType.APPLICATION_JSON_TYPE, "{\"name\":\"\ufeff\u30b8\u30e7\u30f3A\",\"age\":66}"); PUT("http://localhost:8080/dist-http-example/3", MediaType.APPLICATION_JSON_TYPE, "{\"name\":\"adm\",\"age\":88}"); POST("http://localhost:8080/dist-http-example/increment(age,1)", MediaType.APPLICATION_XML_TYPE, null); GET("http://localhost:8080/dist-http-example/1", MediaType.APPLICATION_JSON_TYPE); GET("http://localhost:8080/dist-http-example/1", MediaType.APPLICATION_XML_TYPE); GET("http://localhost:8080/dist-http-example/count()", MediaType.APPLICATION_XML_TYPE); } }