swagger 配置,全局token和说明抽离

/ JAVA / 1030浏览

swagger 配置, 全局token和说明抽离

现在很多开发都开始使用swagger来作为对接接口和测试接口的工具,而且swagger确实真的很好用,但是在很多框架中都需要传令牌来验证登录情况,本文讲一下配置全局token和说明信息抽离到配置文件。

swgger 配置

import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * swagger2 配置
 *
 * @author Create by yachtcay
 * @date 2019/7/12 15:17
 */
@EnableSwagger2
@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties(Swagger2ApiDoc.class)
public class Swagger2Config {

    private final Swagger2ApiDoc apiDoc;

    @Bean
    @SuppressWarnings("all")
    public Docket createRestApi() {
        ParameterBuilder ticketPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        ticketPar.name("Authorization").description("token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .defaultValue("Bearer ")
                .required(true)
                .build();
        pars.add(ticketPar.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);


    }

    //构建 api文档的详细信息函数
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title(apiDoc.getSwagger2().getTitle())
                //创建人
                .contact(new Contact(apiDoc.getSwagger2().getAuthor(), apiDoc.getSwagger2().getUrl(), apiDoc.getSwagger2().getEmail()))
                //版本号
                .version(apiDoc.getSwagger2().getVersion())
                //描述
                .description(apiDoc.getSwagger2().getDescription())
                .build();
    }
}

swagger 与配置文件对应属性


import lombok.Getter;
import lombok.Setter;

/**
 * @author Create by yachtcay
 * @date 2019/7/12 15:29
 */
public class Swagger2Properties {
    /**
     * 作者
     */
    @Getter
    @Setter
    private String author;
    /**
     * 网址
     */
    @Getter
    @Setter
    private String url;
    /**
     * 邮箱
     */
    @Getter
    @Setter
    private String email;
    /**
     * 标题
     */
    @Getter
    @Setter
    private String title;
    /**
     * 版本
     */
    @Getter
    @Setter
    private String version;
    /**
     * 描述
     */
    @Getter
    @Setter
    private String description;
}

yml 或 properties 配置

yml

xx-boot:
  swagger2:
    author: xx-developers
    url: http://www.xxxxxx.com
    email: xxxxxxxxxx@XX.com
    title: xx-boot
    version: 0.0.1
    description: xx-boot XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

properties

xx-boot.swagger2.author=xx-developers
xx-boot.swagger2.url=http=/www.xxxxxx.com
xx-boot.swagger2.email=xxxxxxxxxx@XX.com
xx-boot.swagger2.title=xx-boot
xx-boot.swagger2.version=0.0.1
xx-boot.swagger2.description=xx-boot XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

建立关联


import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * Swagger2 配置信息
 *
 * @author Create by yachtcay
 * @date 2019/7/12 11:03
 */
@ConfigurationProperties(prefix = "xx-boot")
public class Swagger2ApiDoc {

    @Getter
    @Setter
    private Swagger2Properties swagger2 = new Swagger2Properties();
}

配置完成,启动项目看效果

大功告成,可以在swagger中开心的测试bug了