1、pom.xml 依赖
it.geosolutionsgeoserver-manager1.7.0
2、application.properties 配置信息
geoserver.url= http://localhost:8080/geoserver
geoserver.username= admin
geoserver.password= geoserver
3、GeoServerProperties.java 读取配置
@Data
@Configuration
@ConfigurationProperties(prefix = "geoserver")
public class GeoServerProperties {private String url;private String username;private String password;
}
4、GeoServerConfig.java 注入(核心)
@Configuration
public class GeoServerConfig {@Autowiredprivate GeoServerProperties geoServerProperties;@Beanpublic GeoServerRESTManager geoServerRESTManagerFactory() {try {return new GeoServerRESTManager(new URL(geoServerProperties.getUrl()), geoServerProperties.getUsername(),geoServerProperties.getPassword());} catch (Exception ex) {ex.printStackTrace();return null;}}
}
5、测试类
@SpringBootTest
public class GeoServerConfigTest {@Autowiredprivate GeoServerRESTManager geoServerRESTManager;@Testvoid testPublishTif() throws Exception {GeoServerRESTReader geoServerRESTReader = geoServerRESTManager.getReader();String workspace = "xiaozhai";boolean workspaceNull = geoServerRESTReader.existsWorkspace(workspace);System.out.println("workspace是否存在:" + workspaceNull);}
}