knife4j API文档生成

依赖

<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>3.0.3</version>
</dependency>
<dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-annotations</artifactId>
    <version>1.6.5</version>
    <scope>compile</scope>
</dependency>

Controller层

Controller类注解

@Api(value=“Redis”, tags=“”[Car]")

其中value是对该controller下api的描述信息,tags是分类标签,相同tag的api会放在一起展示

@ApiSort(64)

是对api展示顺序的排序,传入的值越小该controller下的api就排在越前面

@ApiVersion(“v1”)

会修改路由,如原RequestMapping指定的路由为/create,加上该注解后请求路由变为/v12/create,用于区别不同版本的api

Controller方法注解

@ApiOperation(“publish message”)

@ApiOperation(value=“publish message”, note=“publish message”)

value为接口简短介绍,note为长说明,note在文档中显示为标题名

@ApiOperationSupport(ignoreParameters = {“id”})

对于添加实体等接口,自动生成id等字段的,可以不需要前端传入id,则配置ignoreParameters让id不会出现在api文档中。

该注解有以下参数:

  • order: 表示方法在页面中显示的排序位置,默认值为0。(原swagger的ApiSort注解不能注解方法)
  • ignoreParameters: 忽略测试参数
  • author: 方法的作者(原swagger的Api注解不能注解方法)
  • group: 方法所属的分组
@ApiResponses
@ApiResponses(value = {
        @ApiResponse(code = 200, message = "成功", response = String.class),
        @ApiResponse(code = 404, message = "channel不存在"),
        @ApiResponse(code = 500, message = "服务器内部错误")
})

在文档里对返回的状态码进行说明(不设置也有几个常见的状态码)

实体层

实体类注解

@ApiModel(value = “User”, description = “用户实体对象”)

value是名字,description是描述

实体属性注解

@ApiModelProperty(value = “用户id”, required = true)

这样在接口文档中,涉及到Car的接口的id项会有required = true的说明

生成文档和其他功能

spring启动后访问http://localhost:8080/doc.html ,可以查看网页版api文档,在配置中开启knife4j.enable=true,可以使用knife4j提供的增强功能:

  • 导出文档的md、word、html、openapi版
  • 可直接复制接口到postman
  • 可设置全局参数,比如修改全局请求header
  • 接口排序,自定文档的展示顺序
  • 配合 Spring Security 可以实现对 API 文档的访问控制
    以下是生成的md和word文件:

https://shimo.im/file/5xkGogOBaaH2dnkX/

https://shimo.im/file/gO3odzVBzgCxxNqD/

其他配置

由于不配置会默认生成所有的接口,包括spring自带的error相关接口和entity自动生成的接口,这些一般不需要出现在接口文档中,因此配置如下,这样将只会扫描com.example.demo.controller包中的接口。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                // 指定Swagger扫描的包路径
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
                .paths(PathSelectors.any())
                .build();
    }
}

此外还可以指定仅生成特定路由前缀的文档,如下为仅扫描以“/api”开头的接口:

@Bean
public Docket api() { 
    return new Docket(DocumentationType.SWAGGER_2)  
        .select()                                  
        .apis(RequestHandlerSelectors.any())              
        .paths(PathSelectors.ant("/api/**"))                          
        .build();                                           
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一种便捷的框架,它可以快速地搭建Java应用程序,并且它对于集成其他组件和框架也十分方便。而Knife4j则是一种集成度很高的API文档工具,它可以将接口文档在Swagger的基础上大幅度优化。在Spring Boot中使用Knife4j整合API文档也非常简单。 首先,我们需要在Spring Boot的项目中引入Knife4j依赖,可以在pom.xml文件中加入以下代码: ``` <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>2.2.7</version> </dependency> ``` 这样Knife4j就会被自动集成到Spring Boot的应用中。 接下来,我们需要在Controller方法上增加注解,并且配置一些信息才能生成接口文档。 ``` @GetMapping("/hello") @ApiOperation(value = "示例API接口", notes = "这是一个示例API接口") @ApiImplicitParams({ @ApiImplicitParam(name = "name", value = "用户名", required = true, dataType = "String", paramType = "header") }) public String hello(@RequestHeader String name){ return "Hello, " + name + "!"; } ``` 其中@GetMapping是Spring Boot的注解,用于标记这是一个GET请求。@ApiOperation和@ApiImplicitParams则是Knife4j的注解,它们分别用于注释方法和方法参数的信息。 最后,在启动Spring Boot应用后,访问http://localhost:8080/doc.html 就可以看到生成的接口文档了。这个文档列表会列出所有接口的URL、HTTP方法、请求参数、响应结果等信息,非常直观和有用。通过Knife4j可以使API文档生成更加高效、直观,方便开发者理解和调用接口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值