1.Springboot整合Swagger2
1.1.pom加swagger依賴
<dependency>
???????? <groupId>io.springfox</groupId>
???????? <artifactId>springfox-swagger2</artifactId>
???????? <version>2.9.2</version>
????? </dependency>
????? <!-- <dependency>
???????? io.springfox
???????? springfox-swagger-ui
???????? 2.9.2
????? -->
????? <dependency>
??????????? <groupId>com.github.xiaoymin</groupId>
??????????? <artifactId>swagger-bootstrap-ui</artifactId>
??????????? <version>1.9.0</version>
??????? </dependency>
????? <dependency>
????? ??? <groupId>io.swagger.core.v3</groupId>
????? ??? <artifactId>swagger-annotations</artifactId>
????? ??? <version>2.0.8</version>
????? </dependency>
??????? <dependency>
????? ??? <groupId>io.swagger.core.v3</groupId>
????? ??? <artifactId>swagger-models</artifactId>
????? ??? <version>2.0.8</version>
</dependency>
1.2.添加swagger配置類
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
//@ConditionalOnExpression("${swagger.enable:true}")
public classSwagger2 {
//??? @Override?
//??? public voidaddResourceHandlers(ResourceHandlerRegistry registry) {?
//???????registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");?
//??????? registry.addResourceHandler("/webjars*").addResourceLocations("classpath:/META-INF/resources/webjars/");?
//??? }?
?? @Value("${swagger.enable:true}")
?? private String enable;
?? @Value("${swagger.basePackage}")
?? private String basePackage;
?? @Bean
?? public Docket createRestApi()
{
????? Predicateselector= PathSelectors.none();
????? if("true".equals(enable)) {
???????? selector=PathSelectors.any();
????? }
????? //定義全局參數(shù)
????? ?Listpars = new ArrayList<>();
????? ??? ParameterBuildertoken = new ParameterBuilder();
????? ??? token.name("token").description("token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
????? ??? pars.add(token.build());
????? return newDocket(DocumentationType.SWAGGER_2)
??????????? .globalOperationParameters(pars) // 全局參數(shù)
??????????? .groupName("demo01")//指定分組
??????????? .apiInfo(apiInfo())
??????????? .select()
??????????? .apis(RequestHandlerSelectors.basePackage(basePackage))
//????????? .paths(PathSelectors.any())
??????????? .paths(selector)
??????????? .build();
?? }
?? private ApiInfo apiInfo() {
????? return new ApiInfoBuilder()
??????????? .title("SpringBoot測試使用 Swagger2 構(gòu)建RESTful API")
??????????? .description("springboot demo 接口文檔")
??????????? .version("1.0")
??????????? .build();
?? }
}
1.3.加上swagger啟用禁用開關(guān)
新建application.properties文件
#swagger啟用禁用標識true:啟用
swagger.enable=true
#swagger掃描需生成文檔的包路徑
swagger.basePackage=com.tjc.springbootDemo.controller
1.4.Swagger具體注釋用法
https://blog.csdn.net/xiaojin21cen/article/details/78654652
1.5本地訪問swagger文檔
訪問原生swagger地址:http://localhost:8080/swagger-ui.html#/
訪問bootStrap-swagger地址:http://localhost:8080/doc.html
訪問json文件數(shù)據(jù)地址:http://localhost:8083/v2/api-docs
1.6.在線訪問swagger接口
https://www.cnblogs.com/vipstone/p/9841716.html
1.7.demo源碼訪問地址
https://gitee.com/JasonTran/swagger2-springbootDemo.git
2.Springmvc整合Swagger2
2.1.pom加依賴
<!-- swagger -->
????? <dependency>
????? ??? <groupId>com.fasterxml.jackson.core</groupId>
????? ??? <artifactId>jackson-annotations</artifactId>
????? ??? <version>2.9.9</version>
????? </dependency>
????? <dependency>
????? ??? <groupId>com.fasterxml.jackson.core</groupId>
????? ??? <artifactId>jackson-core</artifactId>
????? ??? <version>2.9.9</version>
????? </dependency>
????? <dependency>
????? ??? <groupId>com.fasterxml.jackson.core</groupId>
????? ??? <artifactId>jackson-databind</artifactId>
????? ??? <version>2.9.9</version>
????? </dependency>
????? ?<dependency>
???????? <groupId>io.springfox</groupId>
???????? <artifactId>springfox-swagger2</artifactId>
???????? <version>2.9.2</version>
????? </dependency>
????? <!-- <dependency>
???????? io.springfox
???????? springfox-swagger-ui
???????? 2.7.0
????? -->
????? <dependency>
??????????? <groupId>com.github.xiaoymin</groupId>
??????????? <artifactId>swagger-bootstrap-ui</artifactId>
??????????? <version>1.9.0</version>
??????? </dependency>
????? <dependency>
????? ??? <groupId>io.swagger.core.v3</groupId>
????? ??? <artifactId>swagger-annotations</artifactId>
????? ??? <version>2.0.8</version>
????? </dependency>
??????? <dependency>
????? ??? <groupId>io.swagger.core.v3</groupId>
????? ??? <artifactId>swagger-models</artifactId>
????? ??? <version>2.0.8</version>
2.2.添加swagger配置類
@Configuration
@EnableSwagger2
@EnableWebMvc
@EnableSwaggerBootstrapUI
@PropertySource(value="classpath:swagger.properties")
public classSwagger2 {
?? @Value("${swagger.enable:true}")
?? private String enable;
?? @Value("${swagger.basePackage}")
?? private String basePackage;
?? @Bean
?? public Docket createRestApi()
{
????? Predicateselector= PathSelectors.none();
????? //啟用swagger
????? if("true".equals(enable)) {
????? ?? selector=PathSelectors.any();
????? }
//??? //定義全局參數(shù)
//??? List pars = new ArrayList();
//??? ??? ParameterBuilder token = newParameterBuilder();
//??? ???token.name("token").description("token").modelRef(newModelRef("string")).parameterType("header").required(false).build();
//??? ??? pars.add(token.build());
????? return newDocket(DocumentationType.SWAGGER_2)
//????????? .globalOperationParameters(pars)//全局參數(shù)
??????????? .groupName("demo01")//指定分組
??????????? .apiInfo(apiInfo())
??????????? .select()
??????????? .apis(RequestHandlerSelectors.basePackage(basePackage))
//????????? .paths(PathSelectors.any())
??????????? .paths(selector)
??????????? .build();
?? }
?? private ApiInfo apiInfo() {
????? return new ApiInfoBuilder()
??????????? .title("SpringMvc測試使用 Swagger2 構(gòu)建RESTful API")
??????????? .description("springmvc demo 接口文檔")
??????????? .version("1.0")
??????????? .build();
?? }
}
2.3. 創(chuàng)建swagger.properties文件
#swagger啟用禁用標識true:啟用
swagger.enable=true
#swagger掃描需生成文檔的包路徑
swagger.basePackage=com.tjc.springmvcDemo.controller
2.4.springmvc初始化文件添加掃包配置
<!-- 開啟springmvc注解 -->
?? ?<mvc:annotation-driven/>
????<mvc:default-servlet-handler/>
?? <context:component-scanbase-package="com.tjc.springmvcDemo.controller"/>
? <!-- 添加配置文件 -->
? <context:property-placeholderlocation="classpath:swagger.properties"/>
?? <context:component-scanbase-package="com.tjc.springmvcDemo.config.swagger"/>
2.5.Swagger具體注釋用法
https://blog.csdn.net/xiaojin21cen/article/details/78654652
2.6.在線訪問swagger接口
https://www.cnblogs.com/vipstone/p/9841716.html
2.7.demo源碼訪問地址
https://gitee.com/JasonTran/swagger2-springmvcDemo.git
3.問題整理
3.1.項目中攔截器辙培,過濾器攔截了swagger路徑
解決方案:剔除/swagger-resources周伦,/webjars/哩至,/v2/卦碾,/doc.html/等路徑
.excludePathPatterns("/swagger-resources/**","/webjars/**","/v2/**","/doc.html/**");
3.2. guava-${version}.jar包沖突
解決方案:在maven倉庫查找最新guava jar包;
3.3.分布式聚合文檔結(jié)合eureka注冊中心發(fā)現(xiàn)服務,zuul解決瀏覽器跨域佑女,通過調(diào)用swagger的/v2/doc-api接口獲取文檔json數(shù)據(jù)實現(xiàn)文檔聚合管理