2019-11-06

RestTemplate请求Geoserver图层启动停止 通过设置图层基本资源信息中的启用可以设置图层是否可以被访问:
2019-11-06
文章图片
设置图层可否访问 而关于它的详情与修改接口是:


2019-11-06
文章图片
获取图层基本信息

2019-11-06
文章图片
修改图层基本信息 代码如下:
String url ="http://IP:port/geoserver/rest/layers/BOU1_4M_L.xml";
String username ="admin";
String password ="geoserver";
/**
* 获取图层的详细信息
* @param url 访问地址
* @param username 用户名
* @param password 密码
* @return
*/
public static ObjectgetLayers(String url, String username, String password){
Object result =new HashMap();
try {
HttpHeaders headers =new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML));
HttpEntity requestEntity =new HttpEntity(null, headers);
//执行HTTP请求
RestTemplate restTemplate =new RestTemplate();
restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor(username, password));
ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
// 将xml字符串格式化
Document document = DocumentHelper.parseText(response.getBody());
// 获取根节点
Element rootElement = document.getRootElement();
// 获取resource节点
List resourceElements = rootElement.element("resource").elements();
//String href = https://www.it610.com/article/rootElement.element("resource").element("link").attributeValue("href");
// 遍历resource节点获取atom:link节点href属性
for (int i =0; i < resourceElements.size(); i++) {
Element element = resourceElements.get(i);
if(element.getName() =="link"){
String href = https://www.it610.com/article/element.attributeValue("href");
result =getLayerInfo(href,username,password);
break;
}
}
}catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 获取图层数据的详细信息
* @param url 图层地址
* @param username用户名称
* @param password用户密码
* @return
*/
private static ObjectgetLayerInfo(String url, String username, String password){
Object result =new HashMap();
try {
// 设置请求头部相关参数
HttpHeaders headers =new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML));
HttpEntity requestEntity =new HttpEntity(null, headers);
//执行HTTP请求
RestTemplate restTemplate =new RestTemplate();
restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor(username, password));
ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
result =updateLayer(url, username, password, response.getBody());
}catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 更新图层启动/停止
* @param url 请求地址
* @param username 用户名
* @param password 密码
* @param xml 需要更新的xml
* @return
*/
private static ObjectupdateLayer(String url, String username, String password, String xml){
Object result =new HashMap();
try {
// 去除地址尾部的.xml
String putUrl = url.substring(0,url.length()-4);
// 将xml字符串格式化
Document document = DocumentHelper.parseText(xml);
// 获取根节点
Element rootElement = document.getRootElement();
// 获取enabled节点值
String enabled = rootElement.element("enabled").getTextTrim();
if(enabled.equals("true")){
rootElement.element("enabled").setText("false");
}else {
rootElement.element("enabled").setText("true");
}
String putXml = document.asXML();
RestTemplate restTemplate =new RestTemplate();
HttpHeaders headers =new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_XML);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor(username, password));
HttpEntity requestEntity =new HttpEntity(putXml, headers);
//执行HTTP请求
ResponseEntity response = restTemplate.exchange(putUrl, HttpMethod.PUT, requestEntity, Object.class);
result = response;
}catch (Exception e) {
e.printStackTrace();
}
return result;
【2019-11-06】}

    推荐阅读