geoserver自帶rest服務(wù),可以發(fā)布shp,postgis等數(shù)據(jù)源。本文目前只說明怎么通過geoserver的rest發(fā)布postgis表數(shù)據(jù)。
1、maven添加geoserver-manager的依賴。
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>nl.pdok</groupId>
<artifactId>geoserver-manager</artifactId>
<version>1.7.0-pdok2</version>
</dependency>
</dependencies>
2、java代碼
import it.geosolutions.geoserver.rest.GeoServerRESTManager;
import it.geosolutions.geoserver.rest.GeoServerRESTPublisher;
import it.geosolutions.geoserver.rest.decoder.RESTDataStore;
import it.geosolutions.geoserver.rest.decoder.RESTLayer;
import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder;
import it.geosolutions.geoserver.rest.encoder.datastore.GSPostGISDatastoreEncoder;
import it.geosolutions.geoserver.rest.encoder.feature.GSFeatureTypeEncoder;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
public class Temp {
public static void main(String[] args) throws MalformedURLException {
//GeoServer的連接配置
String url = "http://localhost:8080/geoserver" ;
String username = "admin" ;
String passwd = "geoserver" ;
//postgis連接配置
String postgisHost = "localhost" ;
int postgisPort = 5432 ;
String postgisUser = "postgres" ;
String postgisPassword = "19920318" ;
String postgisDatabase = "postgis_24_sample" ;
String ws = "wzf" ; //待創(chuàng)建和發(fā)布圖層的工作區(qū)名稱workspace
String store_name = "wafangdianshi" ; //待創(chuàng)建和發(fā)布圖層的數(shù)據(jù)存儲名稱store
String table_name = "wafangdianshi" ; // 數(shù)據(jù)庫要發(fā)布的表名稱,后面圖層名稱和表名保持一致
//判斷工作區(qū)(workspace)是否存在,不存在則創(chuàng)建
URL u = new URL(url);
GeoServerRESTManager manager = new GeoServerRESTManager(u, username, passwd);
GeoServerRESTPublisher publisher = manager.getPublisher() ;
List<String> workspaces = manager.getReader().getWorkspaceNames();
if(!workspaces.contains(ws)){
boolean createws = publisher.createWorkspace(ws);
System.out.println("create ws : " + createws);
}else {
System.out.println("workspace已經(jīng)存在了,ws :" + ws);
}
//判斷數(shù)據(jù)存儲(datastore)是否已經(jīng)存在,不存在則創(chuàng)建
RESTDataStore restStore = manager.getReader().getDatastore(ws, store_name);
if(restStore == null){
GSPostGISDatastoreEncoder store = new GSPostGISDatastoreEncoder(store_name);
store.setHost(postgisHost);//設(shè)置url
store.setPort(postgisPort);//設(shè)置端口
store.setUser(postgisUser);// 數(shù)據(jù)庫的用戶名
store.setPassword(postgisPassword);// 數(shù)據(jù)庫的密碼
store.setDatabase(postgisDatabase);// 那個數(shù)據(jù)庫;
store.setSchema("public"); //當(dāng)前先默認(rèn)使用public這個schema
store.setConnectionTimeout(20);// 超時設(shè)置
//store.setName(schema);
store.setMaxConnections(20); // 最大連接數(shù)
store.setMinConnections(1); // 最小連接數(shù)
store.setExposePrimaryKeys(true);
boolean createStore = manager.getStoreManager().create(ws, store);
System.out.println("create store : " + createStore);
} else {
System.out.println("數(shù)據(jù)存儲已經(jīng)存在了,store:" + store_name);
}
//判斷圖層是否已經(jīng)存在,不存在則創(chuàng)建并發(fā)布
RESTLayer layer = manager.getReader().getLayer(ws, table_name);
if(layer == null){
GSFeatureTypeEncoder pds = new GSFeatureTypeEncoder();
pds.setTitle(table_name);
pds.setName(table_name);
pds.setSRS("EPSG:4326");
GSLayerEncoder layerEncoder = new GSLayerEncoder();
boolean publish = manager.getPublisher().publishDBLayer(ws, store_name, pds, layerEncoder);
System.out.println("publish : " + publish);
}else {
System.out.println("表已經(jīng)發(fā)布過了,table:" + table_name);
}
}
}