GeoServer provides a series of interfaces for developers to read and write layer data, and there are toolkits in java that encapsulate these rest interfaces
1. Official website interface document
https://docs.geoserver.org/stable/en/user/rest/index.html
Operations on the web interface can be implemented by finding the interface, such as creating storage space, creating data warehouse, publishing layers, querying layers, etc.
A few common interface examples are as follows:
1.1 Publish shp layer
First check the parameter description from the official website API documentation
https://docs.geoserver.org/latest/en/api/#1.0.0/datastores.yaml
Use postman to test first
It should be noted that the request header needs to be verified by user name and password. The
interface verification information is obtained by encrypting the user name:password
String client = "admin:geoserver";
client = Base64.getEncoder().encodeToString(client.getBytes());
System.out.println("Basic " + client);
== It has been tested that after decompressing the compressed package, there must be a shp file instead of a folder, and the Chinese shp package is not supported. The follow-up article will find the problem from the source code and deal with it ==
1.2 Query layer list
https://docs.geoserver.org/latest/en/api/#1.0.0/layers.yaml can query all published layers, and can also query basic layer information
by specified workspace , including links to style details and attribute details
1.3 Query layer feature attribute information
https://docs.geoserver.org/latest/en/api/#1.0.0/featuretypes.yaml
attribute information contains all the information of the layer, including storage warehouse, coordinate system, boundary coordinates, attribute list, point-line-surface type etc.
2. Call interface in JAVA
2.1 Use the native interface call
An unpretentious demo provided by chatGPT, using the shp file released above as an example
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 Toolkit call
1. pom import
<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. The toolkit is easy to use
geoserver create connection information
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();
}