REST Support Module
This documentation is no longer maintained. Please see the new GeoServer documentation at http://docs.geoserver.org
As of r8636 there is a community module named rest in the 1.6.x branch. This module is intended to support the development of RESTlet-based extensions to GeoServer
The Big Picture
The REST philosophy of web service design is centered around giving resources stable identifiers and using standard HTTP methods to manipulate them (ie, GET retrieves a resource, POST creates a new one, etc.). Restlet (http://restlet.org/) is a Java API to facilitate design. This module fits in by helping to tie Restlet into GeoServer in a way that allows jars to add RESTful resources to GeoServer without having to modify anything in the main GeoServer .war file.
How do I use it?
A decent introduction to the Restlet API lives on the restlet project page, http://www.restlet.org/documentation/1.0/firstSteps . For now, you can just create a simple restlet like so:
import org.restlet.Restlet; import org.restlet.data.Request; import org.restlet.data.Response; import org.restlet.data.Status; import org.restlet.data.Method; import org.restlet.resource.StringRepresentation; public class HelloWorldRestlet extends Restlet{ public void handle(Request req, Response resp){ if (req.getMethod().equals(Method.GET)){ resp.setEntity(new StringRepresentation("Hello world!")); } else { resp.setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED); } } }
Now, to tie this into GeoServer you'll need to create a bean in the application context. This is done by modifying (or creating if it doesn't already exist) the applicationContext.xml file for your project. This should live in the base of your source directory, ie. helloworld/src/main/java/applicationContext.xml .
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="restConfigMapping" class="org.geoserver.rest.RESTMapping"> <property name="routes"> <map> <entry> <key><value>/helloworld</value></key> <ref bean="hello"/> </entry> </map> </property> </bean> <bean name="hello" class="HelloWorldRestlet"/> </beans>
Now, building your project and adding the jar to GeoServer's class path will automatically register your restlet. The key in the map is the url you'd like your restlet to have. (The servlet context and a prefix "rest" are added automatically, so this example would register our restlet with the URL http://localhost:8080/geoserver/rest/helloworld . ) Congratulations, you've made your first GeoServer REST plugin!
More Advanced Use
A Little Assistance
One other feature that is common in REST interfaces is to provide multiple formats or versions of the same resource. GeoServer's REST support package includes a couple of classes to help with conversion between different formats.
- MapResource: An abstract class that allows you to choose a standard Map representation to use internally and handles translating between different output formats and Java Maps.
- DataFormat: An interface for classes that translate between Maps and different output formats
- BeanResourceFinder: A Restlet that wraps a Resource object so it can be used with restconfig.