【SpringBoot】集成swagger

1、引入依赖



    io.springfox
    springfox-swagger2
    2.7.0



    io.springfox
    springfox-swagger-ui
    2.7.0

2、配置header(若有token的话,可以采用如下配置)

3、配置包扫描多路径

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.example.demo.constant.StringConstant;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
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;

/**
 * @className Swagger
 * @description swagger集成配置类
 * @author zlt
 * @date 2018/10/23
 * @version 1.0.0
 * @remark
 **/
@Configuration
@EnableSwagger2
@EnableWebMvc
public class SwaggerConfig extends WebMvcConfigurerAdapter {
    @Bean
    public Docket createRestApi() {
        //在请求header头部添加token参数
        ParameterBuilder ticketPar = new ParameterBuilder();
        List pars = new ArrayList<>();
        //name表示名称,description表示描述
        ticketPar.name(StringConstant.HEAD_TOKEN).description("登录校验token")
                .modelRef(new ModelRef("string")).parameterType("header")
                //required表示是否必填
                .required(false).defaultValue("TOKEN默认值").build();
        pars.add(ticketPar.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //可以配置多个包扫描路径
                .apis(SwaggerConfig.basePackage("com.example.demo.controller.department,com.example.demo.controller.user"))
                .paths(PathSelectors.any())
                .build()
                //header配置在全局生效
                .globalOperationParameters(pars);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("XXX接口文档")
                .description("")
                .termsOfServiceUrl("")
                .version("1.0")
                .build();
    }

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

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * @methodName basePackage
     * @description
     * @author zlt
     * @date 2018/10/29
     * @param basePackage 多个包名以逗号隔开
     * @return com.google.common.base.Predicate
     * @version 1.0.0
     **/
    public static Predicate basePackage(final String basePackage) {
        return new Predicate() {
            @Override
            public boolean apply(RequestHandler input) {
                return declaringClass(input).transform(handlerPackage(basePackage)).or(true);
            }
        };
    }

    /**
     * @methodName handlerPackage
     * @description 处理包路径配置规则,支持多路径扫描匹配以逗号隔开
     * @author zlt
     * @date 2018/10/29
     * @param basePackage 扫描包路径
     * @return com.google.common.base.Function,java.lang.Boolean>
     * @version 1.0.0
     **/
    private static Function, Boolean> handlerPackage(final String basePackage) {
        return new Function, Boolean>() {

            @Override
            public Boolean apply(Class input) {
                for (String strPackage : basePackage.split(",")) {
                    boolean isMatch = input.getPackage().getName().startsWith(strPackage);
                    if (isMatch) {
                        return true;
                    }
                }
                return false;
            }
        };
    }

    /**
     * @methodName declaringClass
     * @description 将实例对象转为optional对象
     * @author zlt
     * @date 2018/10/29
     * @param input
     * @return com.google.common.base.Optional>
     * @version 1.0.0
     **/
    private static Optional> declaringClass(RequestHandler input) {
        return Optional.fromNullable(input.declaringClass());
    }
}

 

你可能感兴趣的:(❀❀❀❀❀❀-框架篇)