package com.biubiu.common.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
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;

/**
 * Swagger API文档配置类
 *
 * @author biubiu
 * @date 2020/11/27 23:59
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Value("${spring.profiles.active}")
    private String activeProfile;

    @Bean
    public Docket restFulApi() {
        //添加head参数start
        List<Parameter> pars = createHeaderParameter();
        //添加head参数end
        
        return new Docket(DocumentationType.SWAGGER_2)
                //.enable("dev".equals(activeProfile)) 用于配置线上环境是否显示文档
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }
    //header参数
    private List<Parameter> createHeaderParameter() {
        //1. cityen
        ParameterBuilder cityen = new ParameterBuilder();
        cityen.name("cityen").description("城市简写:sz,tj").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(cityen.build());
        return pars;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("API 文档标题")
                .description("API 文档描述")
                .version("1.0.0")
                .build();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58