springboot swagger starter

自定义starter不会的可以点此链接查看

下面介绍starter的实现

添加依赖


        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger2artifactId>
            <version>2.6.1version>
        dependency>
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger-uiartifactId>
            <version>2.6.1version>
        dependency>

        <dependency>
            <groupId>org.springframework.plugingroupId>
            <artifactId>spring-plugin-coreartifactId>
            <version>1.2.0.RELEASEversion>
            <exclusions>
                <exclusion>
                    <groupId>*groupId>
                    <artifactId>*artifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-spring-webartifactId>
            <version>2.6.1version>
            <exclusions>
                <exclusion>
                    <groupId>*groupId>
                    <artifactId>*artifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-schemaartifactId>
            <version>2.6.1version>
            <exclusions>
                <exclusion>
                    <groupId>*groupId>
                    <artifactId>*artifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>org.springframework.plugingroupId>
            <artifactId>spring-plugin-metadataartifactId>
            <version>1.2.0.RELEASEversion>
            <exclusions>
                <exclusion>
                    <groupId>*groupId>
                    <artifactId>*artifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>io.github.robwingroupId>
            <artifactId>swagger2markupartifactId>
            <version>0.9.2version>
            <exclusions>
                <exclusion>
                    <groupId>*groupId>
                    <artifactId>*artifactId>
                exclusion>
            exclusions>
        dependency>

定义一个SwaggerConfig类




import springfox.documentation.swagger2.annotations.EnableSwagger2;

import org.springframework.context.annotation.Bean;
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 org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@EnableSwagger2
@Configuration
public class SwaggerConfig extends WebMvcConfigurerAdapter {

    @Value("${sop.swagger.enable}")
    private boolean enable;
    @Value("${sop.swagger.package.scan}")
    private String  packageScan;
    @Value("${sop.swagger.title}")
    private String  title;
    @Value("${sop.swagger.description}")
    private String  description;
    @Value("${sop.swagger.version}")
    private String  version;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (enable) {
            registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
    }

    @Bean
    public Docket createRestApi() {
        ApiInfo apiInfo = new ApiInfoBuilder().title(title)//大标题
                .description(description).version(version)//版本
                .build();
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo).enable(enable)
                .useDefaultResponseMessages(false).select().apis(RequestHandlerSelectors.basePackage(packageScan))
                .build();
    }
}

在resources下创建META-INF/spring.factories

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.swagger.SwaggerConfig

然后starter就可以直接打包放在私服上去了

使用方式

在项目中添加starter依赖

在application.properties
#swagger
sop.swagger.enable=true 
sop.swagger.package.scan=com.demo
sop.swagger.title=标题
sop.swagger.description=描述
sop.swagger.version=3.0

你可能感兴趣的:(springboot swagger starter)