Using Restlet in GeoServer to provide all the rest services, and all requests in the/rest directory in GeoServer are treated as a restful server, which is the call procedure for the rest service
When creating a new project, you need to create a new Pom.xml to the Meven project
<projectxmlns="http://maven.apache.org/POM/4.0.0"Xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="Http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelversion>4.0.0</modelVersion><groupid>org.geoserver</groupId><artifactid>hello_rest</artifactId><packaging>jar</packaging><version>1.0-snapshot</version><name>hello_rest</name><dependencies><dependency><groupid>org.geoserver</groupId><artifactid>gs-rest</artifactId><version>2.8-snapshot</version><!--change this to the proper GeoServer version--</dependency><dependency><groupid>org.geoserver</groupId><artifactid>gs-main</artifactId><version>2.8-snapshot</version><!--change this to the proper GeoServer version--<classifier>tests</classifier><scope>test</scope></dependency><dependency><groupid>junit</groupId><artifactid>junit</artifactId><version>4.11</version><scope>test</scope></dependency><dependency><groupid>com.mockrunner</groupId><artifactid>mockrunner</artifactid> <version >0.3.6</version> <scope>test</scope> Span class= "NT" ></dependency> </dependencies> <build> <plugins> <plugin> <artifactid> Maven-compiler-plugin</artifactid> <configuration> <source>1.8</source> <target>1.8 </target> </configuration> </plugin> </plugins> </build></PROJECT>
When you build a project, you need to inherit a class and implement one of the methods
-
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 a Bstractresource.
PackageOrg.GeoServer.Hellorest;ImportJava.util.List;ImportOrg.geoserver.rest.AbstractResource;ImportOrg.geoserver.rest.format.DataFormat;import org.restlet.data.request; Import org.restlet.data.response; Public class helloresource extends Abstractresource { @Override protected list<dataformat> createsupportedformats (request requestresponse response) {return null } /span>
-
The first method to implement is createsupportedformats (). The purpose of this method are to create mapping from an extension, to a particular format. For now the goal would be to return the text ' Hello world ' when a ". txt" extension is requested by the client.
ImportJava.util.ArrayList;ImportOrg.geoserver.rest.format.StringFormat;...@OverrideProtectedList<DataFormat> createsupportedformats (request requestresponse response {list<dataformat> formats = new arraylist (); formats. (new stringformat (MediaType< Span class= "O". text_plain return formats;}
-
the next step is to override the handleget () method. This method was called when a GET request was made for the resource.
@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 are to determine the extension being requested by The client, and look up the appropriate format for it. In the If the client requests the ". txt" extension, the StringFormat setup in the previous step would be F Ound.
Create a application context
-
The next step is to create a application context that tells GeoServer on 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< C4>hello_rest module.
<! DOCTYPE beans Public "-//spring//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd" ><beans><beanid= "hello" class= " Org.geoserver.hellorest.HelloResource "/> <bean id= "hellomapping" class= "org.geoserver.rest.RESTMapping" > <property name= "routes" > <map> <entry> <key> <value>/hello. {format} </value></key> <value>hello</value> Span class= "NT" ></entry> </map> </property> </bean></BEANS>
There is things to note above. The first is the Hello bean which is a instance of the Helloresourceclass created in the previous secti On. The second is the hellomapping Bean, which defines a template for the URI in which the resource would be accessed. The above mapping specifies that the resource would be is located at /rest/hello.{ Format} where format is the representation being requested by the client. As implemented Hello.txt is the only supported representation
GeoServer Rest Service Introduction, build Java program