How to create and register a new REST service from a plugin

These are instructions for registering a REST service in a plugin.  The following class EchoDatasourceService is an example of a REST service implementation.

package org.pentaho.datasource.samples;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.DELETE;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.MediaType.WILDCARD;
import static javax.ws.rs.core.MediaType.APPLICATION_XML;

import org.pentaho.platform.web.http.api.resources.JaxbList;

@Path("/echo/api/datasource")
public class EchoDatasourceService {
List<String> echoDatasourceList = new ArrayList<String>();
public EchoDatasourceService()

{ echoDatasourceList.add("EchoTestDatasource_1"); echoDatasourceList.add("EchoTestDatasource_2"); echoDatasourceList.add("EchoTestDatasource_3"); echoDatasourceList.add("EchoTestDatasource_4"); }
@PUT
@Path("/add")
@Consumes(

{ WILDCARD }
)
@Produces("text/plain")
public Response add(String name)

{ echoDatasourceList.add(name); return Response.ok("TRUE").type(MediaType.TEXT_PLAIN).build(); }
@GET
@Path("/ids")
@Produces(

{ APPLICATION_XML, APPLICATION_JSON }
)
public JaxbList<String> getIds()

{ return new JaxbList<String>(echoDatasourceList); }
@DELETE
@Path("/remove")
@Consumes(

{ WILDCARD }
)
@Produces("text/plain")
public Response remove(String name)

{ echoDatasourceList.remove(name); return Response.ok("TRUE").type(MediaType.TEXT_PLAIN).build(); }
}

In this class, we defined a class name, methods and how they can be accessed via REST. Notice the annotation above the class definition @Path("/echo/api/datasource"). This is where you define how par of the url for this REST service. All plugin REST service urls starts with http://localhost:8080/pentaho/plugin and then the path that you will define in the annotation. Every method in the REST service also has a path annotation, which is relative from the main path annotation of the REST service class. So if you want to invoke getIds method, the REST url will be http://localhost:8080/pentaho/plugin/echo/api/datasource/ids.

We are not done yet, the final piece of the puzzle is registering this new REST service in the plugin.spring.xml

     <!--
     5. Here we configure a resource-based service called EchoResource.
        The JAX-RS @Path annotation completes the URI to this resource.
        @Path should be given the value /<plugin>/<JAXRSPluginServlet-id>/<name-of-resource>
        In this case, the name of the resource is 'datasource', so the complete URI is
        http://<host>:<port>/pentaho/plugin/echo/api/datasource
      -->
     <bean id="api" class="org.pentaho.platform.web.servlet.JAXRSPluginServlet"/>
     <!-- The actual resource class need not have a bean id. Classes will be scanned for JAX-RS annotations
          and this one will be found. -->
	 <bean class="org.pentaho.datasource.samples.EchoDatasourceService"/>

You will need to add a bean with id "api" and class name "org.pentaho.platform.web.servlet.JAXRSPluginServlet". The next item is to add the class we just created EchoDatasourceService. Now we are all done. Deploy the plugin into the platform and your REST service will be available now.