REST Services in GateIn / eXoPlatform : add your own !

Mulitple REST services are available out of the box in GateIn / eXoPlatform. They allow to easily interact with the platform’s features and data.

Here we will see how to quickly add your own REST services (valid for eXoPlatform 3.0 or 3.5).

Java REST class

Firstly, we create the Java class which will expose the REST service :

package org.exoplatform.samples.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

import org.exoplatform.services.rest.resource.ResourceContainer;

@Path("/user/")
public class MyRESTService implements ResourceContainer {

    @Path("/name/{name}/")
    @GET
    public Response getName(@PathParam("name") String name) throws Exception {
        return Response.ok("Hello " + name + " !").build();
    }

}

As you can see, this class uses standard JAX-RS annotations to expose its services (javax.ws.rs.*). The only thing to add is the org.exoplatform.services.rest.resource.ResourceContainer interface. This interface is a marker to define this class as a REST services class.

If you use Maven, the org.exoplatform.services.rest.resource.ResourceContainer class is available with the following dependency :

<dependency>
  <groupId>org.exoplatform.ws</groupId>
  <artifactId>exo.ws.rest.core</artifactId>
  <version>2.2.6-GA</version>
  <scope>provided</scope>
</dependency>

Adjust the dependency version according to your Gatein / eXoPlatform version.

Declare your REST service

The second and last thing to do is to declare the REST service. To achieve this, declare your class in the GateIn / eXoPlatform configuration :

<component>
  <type>org.exoplatform.samples.rest.MyRESTService</type>
</component>

This configuration can be put either in the jar containing the java class (in conf/portal/configuration.xml), or in an extension, or even in the external configuration.

Now you just have to deploy your class and your configuration (for example, put the jar containing the class and the configuration in the tomcat’s libs), and restart the server.
Your service is now available at http://<host&gt;:<port>/portal/rest/user/name/<yourname> :

Up to you now to implement your custom REST service !

About these ads
  1. #1 by Mallikarjun Ps on 27/09/2013 - 17:04

    thank you for the post. I am building a custom rest service in exo platform and my service dependent on some third party jars. instead of copying these jars manually into tomcat lib folder , is there a I can ship them along with my service. I created a extension project with war containing these libraries but for some reason these jars are not recognized buy exo while running my services and throwing ClassDefNotFound error.

    Thanks
    Mallikarjun

  1. eXo Blog – The Cloud User Experience Platform – Boost your eXo Platform Development with JRebel!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: