2021-10-10

【Java】SpringBoot 之文档生成

part 1引入依赖

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <!-- &&<artifactId>springfox-swagger-ui</artifactId> -->
            <version>2.9.2</version> 
        <dependency>
<!--如果需要引入 UI 界面 则需要另外引入注释部分依赖-->            

part 2配置

import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration	//前提 : 声明是配置文件类
@EnableSwagger2//作用在于开启 Swagger
public class SwaggerConfig {
    /**
     * 使用 Swagger 需要创建一个摘要
     *  参数有
     *      文档类型——DocumentationType.SWAGGER_2、
     *      而文档是通过一系列选择器组成的 api path
     *
     *      apis 代表生成哪些 controller 的接口
     *      paths 在查找出来的接口进行筛选
     *
     *		如果只需要获取一部分接口 则 apis 的参数可写为
     *		RequestHandlerSelectors.basePackage("com")
     *			包名.....
     * */
    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }
	//可以设置相关信息 如标题 版本 描述
    public ApiInfo apiInfo(){
        return new ApiInfoBuilder().title("田浩然")
                .version("1.0.0")
                .description("牛逼")
                .build();
    }
}

apiInfo 展示

2021-10-10

几个注解

@Api

​ 对接口进行描述,比如标签之类的

@ApiOperation

​ 对接口的方法进行描述

@ApiModel

​ 见名知意–声明实例说明

@ApiModelProperty

见名知意–声明实例属性

@ApiIgnore

​ 忽略某些接口

@ApiImplicitParam

part 3访问

无 UI

http://localhost:8080/v2/api-docs

有 UI

http://localhost:8080/swagger-ui.html

效果展示

2021-10-10

上一篇:java-Swagger参数文档


下一篇:使用swagger2生成文档