Springboot整合Swagger2配置

1、导入依赖

 <!-- Swagger API文档 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
            <exclusions>
                <exclusion>
                    <groupId>io.swagger</groupId>
                    <artifactId>swagger-annotations</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>io.swagger</groupId>
                    <artifactId>swagger-models</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>
        <!-- # 增加两个配置解决 NumberFormatException -->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.22</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.5.22</version>
        </dependency>

2、Swagger2Config.java的配置

@Slf4j
@Configuration
@EnableWebMvc
@EnableSwagger2
@ComponentScan(basePackages = "com.ma.controller")
public class Swagger2Config implements WebMvcConfigurer {

    @Bean
    public Docket controller_api(){

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())//接口文档信息
                .select() //查找
                .apis(RequestHandlerSelectors.basePackage("com.ma.controller")) //生成文档的包
                .paths(PathSelectors.any()) //路径
                .build() //构建
                .groupName("控制类接口"); //组名
    }

    @Bean
    public Docket domain_api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ma.domain"))
                .paths(PathSelectors.any())
                .build()
                .groupName("实体类");
    }

    @Bean
    public Docket untiy_api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ma.untiy"))
                .paths(PathSelectors.any())
                .build()
                .groupName("工具类");
    }




    public ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("第一个接口文档")//接口文档标题
                .version("v-1")//版本
                .description("这是第一个接口文档测试")//接口文档描述
                .license("The Apache License, Version 2.0")//执照
                .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html") //执照地址
                .contact(new Contact("张三","https://www.baidu.com","17855454@qq.com")) //团队
                .build(); //构建接口文档信息
    }

    /**
     *
     * 显示swagger-ui.html文档展示页,还必须注入swagger资源:
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

3、在需要生成文档的类上加上注解

@Api(tags = "控制类") //在接口上添加注解
@ApiOperation(value = "返回值方法")//在方法上添加注解
@ApiModelProperty(value = "id") //实体类字段上的注解
上一篇:Swagger


下一篇:Swagger-01-快速入门