knife4j API文档生成使用流程及详解

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

初始化流程

        1pom文件引入相关依赖

<!--api接口文档生成-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
            <version>4.0.0</version>
        </dependency>

2创建配置类

package com.example.springboot_demo.config;

/**
 *
 */
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {
    //配置Swagger2的Docket的Bean实例
    @Bean public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                // apiInfo()配置 API 的一些基本信息比如文档标题title文档描述 description文档版本号version
                .apiInfo(apiInfo())
                // select()生成 API 文档的选择器用于指定要生成哪些 API 文档
                .select()
                // apis()指定要生成哪个包下的 API 文档
                .apis(RequestHandlerSelectors.basePackage("com.example.springboot_demo.controller"))
                // paths()指定要生成哪个 URL 匹配模式下的 API 文档。这里使用 PathSelectors.any()表示生成所有的 API 文档。
                .paths(PathSelectors.any()) .build();
    }

    //文档信息配置
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                // 文档标题 .title("微博项目") // 文档描述信息
                .description("微博项目在线API文档")
                // 文档版本号 .version("1.0")
                .build(); }
}

3默认访问域名+端口+doc.html

详细注解分析

@Api

        类上

        tags配置模块的名称

@ApiOperation

        方法上

        value配置业务名称

@ApiModelProperty

        bean类属性上

        value参数中文名称

        required参数是否必填

        example配置示例值

@ApiImplicitParam

        方法上

        name指定参数名称

        value配置参数名称

        dataType配置数据类型

        required是否必填true/false

        example示例值

@ApiImplicitParams

        value参数值  示例

{
    @ApiImplicitParam(name = "id", value = "微博", required=true, 

dataType = "int"),
    @ApiImplicitParam(name = "username", value = "用户名", required=true)
}

@ApiIgnore

        添加在处理请求的方法的参数上用于表示API文档框架应该忽略此参数

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6