.. _rest_services_implementing:
Implementing a RESTful Service
==============================
This section describes how to implement a restful service in GeoServer, using
a "Hello World" example. The service will be extremely simple and will return
the text "Hello World" from a GET request.
Prerequisites
--------------
Before being able to proceed, GeoServer must be built on the local system. See
the :ref:`source` and :ref:`quickstart` sections for details.
Create a new module
-------------------
#. Create a new module named ``hello_rest`` somewhere on the file system.
#. Add the following ``pom.xml`` to the root of the new module:
.. code-block:: xml
4.0.0org.geoserverhello_restjar1.0-SNAPSHOThello_restorg.geoservergs-rest2.8-SNAPSHOTorg.geoservergs-main2.8-SNAPSHOTteststestjunitjunit4.11testcom.mockrunnermockrunner0.3.6testmaven-compiler-plugin1.8
#. Create the directory ``src/main/java`` under the root of the new module::
[hello_rest]% mkdir -p src/main/java
Create the resource class
-------------------------
#. The class ``org.geoserver.rest.AbstractResource`` is a convenient base
class available when creating new resources. Create a new class called
``HelloResource`` in the package ``org.geoserver.hellorest``, which
extends from ``AbstractResource``.
.. code-block:: java
package org.geoserver.hellorest;
import java.util.List;
import org.geoserver.rest.AbstractResource;
import org.geoserver.rest.format.DataFormat;
import org.restlet.data.Request;
import org.restlet.data.Response;
public class HelloResource extends AbstractResource {
@Override
protected List createSupportedFormats(Request request, Response response) {
return null;
}
}
#. The first method to implement is ``createSupportedFormats()``. The purpose
of this method is to create mapping from an extension, to a particular
format. For now the goal will be to return the text "Hello World" when a
".txt" extension is requested by the client.
.. code-block:: java
import java.util.ArrayList;
import org.geoserver.rest.format.StringFormat;
...
@Override
protected List createSupportedFormats(Request request, Response response) {
List formats = new ArrayList();
formats.add(new StringFormat( MediaType.TEXT_PLAIN ));
return formats;
}
#. The next step is to override the ``handleGet()`` method. This method is
called when a GET request is made for the resource.
.. code-block:: java
@Override
public void handleGet() {
//get the appropriate format
DataFormat format = getFormatGet();
//transform the string "Hello World" to the appropriate response
getResponse().setEntity(format.toRepresentation("Hello World"));
}
The above makes use of the ``getFormatGet()`` method, whose purpose is
to determine the extension being requested by the client, and look up
the appropriate format for it. In this case when the client requests the
".txt" extension, the ``StringFormat`` setup in the previous step will be
found.
Create the application context
------------------------------
#. The next step is to create an application context that tells GeoServer
about the resource created in the previous section. Create the directory
``src/main/resources`` under the root of the ``hello_rest`` module::
[hello_rest]% mkdir src/main/resources
#. Add the following ``applicationContext.xml`` file to the ``src/main/resources`` directory under the root of the ``hello_rest`` module.
.. code-block:: xml
There are two things to note above. The first is the ``hello`` bean which
is an instance of the ``HelloResource`` class created in the previous
section. The second is the ``helloMapping`` bean, which defines a template
for the uri in which the resource will be accessed. The above mapping
specifies that the resource will be located at ``/rest/hello.{format}``
where ``format`` is the representation being requested by the client. As
implemented ``hello.txt`` is the only supported representation.
Test
----
#. Create the directory ``/src/test/java`` under the root of the
``hello_rest`` module::
[hello_rest]% mkdir -p src/test/java
#. Create a new test class called ``HelloResourceTest`` in the package
``org.geoserver.hellorest``, which extends from
``org.geoserver.test.GeoServerTestSupport``:
.. code-block:: java
package org.geoserver.hellorest;
import org.geoserver.test.GeoServerTestSupport;
public class HelloResourceTest extends GeoServerTestSupport {
public void test() throws Exception {
}
}
#. Add a statement which makes a GET request for ``/rest/hello.txt`` and
asserts it is equal to the string "Hello World":
.. code-block:: java
public void test() throws Exception {
assertEquals( "Hello World", getAsString("/rest/hello.txt"));
}
#. Build and test the ``hello_test`` module::
[hello_rest]% mvn install