geoserver的rest服务介绍,搭建java程序

在geoserver中使用 Restlet 来提供所有的rest服务,并且geoserver中所有的在/rest目录下的请求都被看作为一个restful server,下图就是rest服务的调用过程

 

geoserver的rest服务介绍,搭建java程序_第1张图片
当新建一个项目的时候需要新建一个 pom.xml到meven项目中


  4.0.0

  org.geoserver
  hello_rest
  jar
  1.0-SNAPSHOT
  hello_rest

  
    
      org.geoserver
      gs-rest
      2.8-SNAPSHOT 
    
    
      org.geoserver
      gs-main
      2.8-SNAPSHOT 
      tests
      test
    
    
      junit
      junit
      4.11
      test
    
    
      com.mockrunner
      mockrunner
      0.3.6
     test
    

  

  
    
      
        maven-compiler-plugin
        
          1.8
          1.8
        
     
   
  



建立项目的时候需要继承一个类,以及实现其中的方法
  1. 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.

    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;
       }
    }
    
  2. 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.

    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;
    }
    
  3. The next step is to override the handleGet() method. This method is called when a GET request is 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 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.

创建一个application context

  1. 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
  2. Add the following applicationContext.xml file to the src/main/resources directory under the root of thehello_rest module.

     
    
     
        
    
        
           
              
                
                  /hello.{format}
                  hello
                
             
          
        
    
    
    

    There are two things to note above. The first is the hello bean which is an instance of the HelloResourceclass 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

 

欢迎大家来我的新家看一看 3wwang个人博客-记录走过的技术之路

你可能感兴趣的:(geoserver的rest服务介绍,搭建java程序)