研究了很久,利用WFSClient来调用WFS服务进行增删改查,先上段代码,其他后续补全吧。
//自定义xml的格式来实现
Map connectionParameters = Maps.newHashMap();
//connectionParameters.put("WFSDataStoreFactory:GET_CAPABILITIES_URL",)
HTTPClient httpClient =new SimpleHttpClient();
httpClient.setConnectTimeout(10);
httpClient.setTryGzip(true);
WFSConfig wfsConfig =new WFSConfig();
String url =
geoServerConfig.getUrl() +"/ows?service=wfs&version=1.0.0&request=GetCapabilities";
WFSClient wfsClient =new WFSClient(new URL(url), httpClient, wfsConfig);
TransactionRequest transactionRequest = wfsClient.createTransaction();
//transactionRequest.setOutputFormat("json");
QName typeName =new QName("http://www.xxxx.xxx", "postgis_rd");
transactionRequest.setTypeName(typeName);
Insert insert = transactionRequest.createInsert(typeName);
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
LineString lineString = geometryFactory
.createLineString(new Coordinate[]{new Coordinate(0, 0), new Coordinate(1, 1)});
SimpleFeatureTypeBuilder typeBuilder =new SimpleFeatureTypeBuilder();
typeBuilder.setName("postgis_rd");
typeBuilder.setNamespaceURI("http://wwww.gis.hezhang.yunlizhihui.com/ylgis");
typeBuilder.setSRS("EPSG:4326");
typeBuilder.setCRS(CRS.decode("EPSG:4326"));
typeBuilder.add("name", String.class);
typeBuilder.add("line", LineString.class, CRS.decode("EPSG:4326"));
SimpleFeatureType simpleFeatureType = typeBuilder.buildFeatureType();
SimpleFeatureBuilder simpleFeatureBuilder =new SimpleFeatureBuilder(simpleFeatureType);
simpleFeatureBuilder.set("name", "测试");
simpleFeatureBuilder.set("line", lineString);
SimpleFeature feature = simpleFeatureBuilder.buildFeature(null);
insert.add(feature);
transactionRequest.add(insert);
TransactionResponse transactionResponse = wfsClient.issueTransaction(transactionRequest);
List fids = transactionResponse.getInsertedFids();