Geoserver提供了很多的接口,如果需要给第三方提供接口服务时,可以集成Swagger快速的生成接口帮助,主要步骤如下:
目录
1.配置依赖库
可以通过新建一个Maven模块集成swagger,本文主要介绍在【restconfig】中集成
(1)在restconfig/src/pom.xml中添加依赖
<!--启用swagger add by lyh on 20210508-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
2.配置类
(1)在restconfig/src/main/java/org/geosever/rest下添加swagger包
(2)在wagger包下添加SwaggerConfgi.java类,添加如下代码
package org.geoserver.rest.swagger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableWebMvc
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
Docket dock =
new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("org.geoserver.rest.catalog"))
.build()
.apiInfo(apiInfo())
.ignoredParameterTypes(
org.geotools.styling.Stroke.class,
freemarker.template.Template.class,
org.geotools.ows.wms.Layer.class,
org.geotools.ows.wmts.model.WMTSLayer.class);
return dock;
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Hgisserver Rest API docs")
.description("RESTful API description")
.version("3.6.4")
.build();
}
}
3.资源映射
(1)在restconfig/src/main/resources/applicationContext.xml配置文件添加:
<!--启用swagger add by lyh for swagger doc -->
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />
4.生成项目
(1)重新生成maven项目
(2)Start,出现如下则代表集成成功
5.错误解决方案
初次运行会出现:Hide Fetch errorNot Found http://localhost:8888/postserver/v2/api-docs
解决办法在:AdvancedDispatchFilter.java类中添加如下代码