GeoServer系列-REST接口初识

GeoServer 提供了一系列接口可供开发者读写图层数据,java中也有工具包封装了这些rest接口

1,官网接口文档

https://docs.geoserver.org/stable/en/user/rest/index.html
在这里插入图片描述

web界面上的操作都可以找到接口实现,如创建存储空间,创建数据仓库,发布图层,查询图层等
举几个常见的接口例子如下:

1.1 发布shp图层

先从官网API文档上查看参数描述
https://docs.geoserver.org/latest/en/api/#1.0.0/datastores.yaml
在这里插入图片描述
使用postman先测试下
在这里插入图片描述

需要注意请求头需要加用户名密码的验证
在这里插入图片描述
接口验证信息使用 用户名:密码 加密得到

String client = "admin:geoserver";
client = Base64.getEncoder().encodeToString(client.getBytes());
System.out.println("Basic " + client);

在这里插入图片描述
== 经测试压缩包解压后要有shp文件而不是文件夹,不支持中文shp包,不支持中文的问题后续文章会从源码找问题并处理 ==

1.2 查询图层列表

https://docs.geoserver.org/latest/en/api/#1.0.0/layers.yaml
可以查询所有发布图层,也可以按指定工作空间查询
在这里插入图片描述
在这里插入图片描述
图层基本信息,包含样式详情和属性详情链接
在这里插入图片描述

1.3 查询图层特征属性信息

https://docs.geoserver.org/latest/en/api/#1.0.0/featuretypes.yaml
属性信息包含图层的所有信息,包含存储仓库,坐标系,边界坐标,属性列表,点线面类型等等
在这里插入图片描述

2,JAVA 中调用接口

2.1使用原生接口调用

chatGPT提供的一个朴实无华的demo,用上面发布shp文件举例

package example;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class GeoServerRESTAPIExample {
    public static void main(String[] args) throws IOException {
        String gsUrl = "http://127.0.0.1:8993/geoserver";
        String username = "admin";
        String password = "geoserver";
        String workspace = "wsName";
        String storeName = "shpstoreName";
        String shpFile = "C:\\Users\\CDLX\\Desktop\\EJDL\\EJDL.zip";

        // Encode username and password to Base64
        String auth = Base64.getEncoder().encodeToString((username + ":" + password).getBytes(StandardCharsets.UTF_8));

        // Construct the REST API endpoint URL
        String endpointUrl = String.format("%s/rest/workspaces/%s/datastores/%s/file.shp?configure=all", gsUrl, workspace, storeName);

        // Read the Shapefile ZIP file
        File file = new File(shpFile);
        byte[] fileBytes = new byte[(int) file.length()];
        InputStream inputStream = new FileInputStream(file);
        inputStream.read(fileBytes);
        inputStream.close();

        // Construct the HTTP request
        HttpURLConnection conn = (HttpURLConnection) new URL(endpointUrl).openConnection();
        conn.setRequestMethod("PUT");
        conn.setDoOutput(true);
        conn.setRequestProperty("Authorization", "Basic " + auth);
        conn.setRequestProperty("Content-type", "application/zip");
        conn.getOutputStream().write(fileBytes);

        // Execute the HTTP request and get the response
        int responseCode = conn.getResponseCode();
        String responseMessage = conn.getResponseMessage();

        // Print the response code and message
        System.out.printf("Response code: %d, message: %s%n", responseCode, responseMessage);
    }
}

2.2 工具类包调用

1,pom引入

		<dependency>
            <groupId>it.geosolutions</groupId>
            <artifactId>geoserver-manager</artifactId>
            <version>${geoserver.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-shapefile</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-geojson</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-epsg-hsql</artifactId>
            <version>${geotools.version}</version>
        </dependency>
	。。。
 	<repositories>
        <repository>
            <id>GeoSolutions</id>
            <url>http://maven.geo-solutions.it/</url>
        </repository>
        <repository>
            <id>osgeo</id>
            <name>OSGeo Release Repository</name>
            <url>https://repo.osgeo.org/repository/release/</url>
            <snapshots><enabled>false</enabled></snapshots>
            <releases><enabled>true</enabled></releases>
        </repository>
        <repository>
            <id>osgeo-snapshot</id>
            <name>OSGeo Snapshot Repository</name>
            <url>https://repo.osgeo.org/repository/snapshot/</url>
            <snapshots><enabled>true</enabled></snapshots>
            <releases><enabled>false</enabled></releases>
        </repository>
    </repositories>

2, 工具包简单使用

geoserver创建连接信息

	String url = "http://localhost:8080/geoserver";
	String user= "admin";
	String password= "geoserver";//连接geoServer
	GeoServerRESTManager geoServerRESTManager = null;
	try{
		geoServerRESTManager = new GeoServerRESTManager(newURL(url), user, password);
		assert geoServerRESTManager != null;
		//读写相关操作的对象
		GeoServerRESTReader restReader=geoServerRESTManager.getReader();
		//发布相关操作的对象
		GeoServerRESTPublisher restPublisher= geoServerRESTManager.getPublisher();
	}catch(Exception e) {
		System.out.println("远程连接GeoServer失败...");
		e.printStackTrace();
	}

在这里插入图片描述
在这里插入图片描述