SpringBoot整合Knife4j展示更美观的API文档
SpringBoot整合Knife4j展示更美观的API文档
一、什么是knife4j
knife4j是为Java MVC框架集成Swagger生成Api文档的增强解决方案,前身是swagger-bootstrap-ui, 可以简单理解为它是Swagger的一个UI增强版,该UI增强包主要包括两大核心功能:文档说明 和 在线调试;
-
文档说明:根据Swagger的规范说明,详细列出接口文档的说明,包括接口地址、类型、请求示例、请求参数、响应示例、响应参数、响应码等信息,使用swagger-bootstrap-ui能根据该文档说明,对该接口的使用情况一目了然。
-
在线调试:提供在线接口联调的强大功能,自动解析当前接口参数,同时包含表单验证,调用参数可返回接口响应内容、headers、Curl请求命令实例、响应时间、响应状态码等信息,帮助开发者在线调试,而不必通过其他测试工具测试接口是否正确,简介、强大。
二、整合试用
1. 创建一个普通SpringBoot项目
pom.xml依赖如下:
只需要引入一个依赖即可:<artifactId>knife4j-spring-boot-starter</artifactId>
项目结构如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.linwei.knife4j.demo</groupId> <artifactId>knife4j-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>knife4j-demo</name> <description>Demo project for knife4j</description> <properties> <java.version>1.8</java.version> <knife4j.version>2.0.2</knife4j.version> <fastjson.version>1.2.68</fastjson.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--knife4j依赖--> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>${knife4j.version}</version> </dependency> <!-- fastJson(used for serialization of DAG) --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
2. 和以前Swagger一样,添加一个配置类;
package com.linwei.knife4j.demo.cofig; import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j; 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.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * @author: Linwei * @date 2021/6/15 * @Description: * 配置类和以前使用swagger几乎是一样; * 注解比原来的swagger多加一个 @EnableSwaggerBootstrapUi */ @Configuration @EnableSwagger2 @EnableKnife4j public class SwaggerConfig { @Bean(value = "defaultApi2") public Docket defaultApi2() { Docket docket=new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) //分组名称 .groupName("1.0版本") .select() //这里指定Controller扫描包路径(项目路径也行) .apis(RequestHandlerSelectors.basePackage("com.linwei.knife4j.demo.controller")) .paths(PathSelectors.any()) .build(); return docket; } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("XXX项目API接口文档") .description("API服务相关接口") .termsOfServiceUrl("http://localhost:8888/") .contact(new Contact("Linwei",null,"Linwei.Lu@123456.com")) .version("1.0") .build(); } }
3. 准备测试实体和Controller
3.1 定义一个接口通用的返回对象 CommonResults
package com.linwei.knife4j.demo.com.linwei.knife4j.demo.entity; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.experimental.Tolerate; /** * @author: Linwei * @date 2021/6/15 * @Description: 通用接口返回对象 */ @Data @Builder @AllArgsConstructor @ApiModel("接口通用返回对象") public class CommonResults<T> { private static final long serialVersionUID = 1L; @ApiModelProperty(required = true,notes = "结果码", example = "200") private int code; @ApiModelProperty(required = true,notes = "返回信息", example = "Success") private String message; @ApiModelProperty(required = false,notes = "返回数据", example = "{\"name:\":\"Jack\"}") private T data; @Tolerate public CommonResults() { } public static CommonResults ok() { return CommonResults.builder().code(200).message("请求成功").build(); } public static CommonResults ok(Object data) { return CommonResults.builder().code(200).message("请求成功").data(data).build(); } public static CommonResults error(String message) { return CommonResults.builder().code(500).message("响应异常").build(); } }
3.2 定义一个测试实体-UserEntity
package com.linwei.knife4j.demo.com.linwei.knife4j.demo.entity; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import jdk.nashorn.internal.objects.annotations.Constructor; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; /** * @author: Linwei * @date 2021/6/15 * @Description: */ @Data @ToString @AllArgsConstructor @NoArgsConstructor @ApiModel("用户对象模型") public class UserEntity { @ApiModelProperty(value="姓名" ,required= true,example = "Tom") private String name; @ApiModelProperty(value="年龄" ,required= true,example = "18") private Integer age; @ApiModelProperty(value="id" ,required= true,example = "123456") private Integer id; }
3.3 添加一个controller来测试
package com.linwei.knife4j.demo.controller; import com.linwei.knife4j.demo.com.linwei.knife4j.demo.entity.CommonResults; import com.linwei.knife4j.demo.com.linwei.knife4j.demo.entity.UserEntity; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.*; /** * @author: Linwei * @date 2021/6/15 * @Description: */ @RestController @RequestMapping("/user") @Api("用户接口") public class UserController { @GetMapping("/getbyid") @ApiOperation("根据id获取用户信息接口") public CommonResults getUser(@RequestParam Integer id){ UserEntity user = new UserEntity("Jerry",10,1111); return CommonResults.ok(user); } @PostMapping("/user/add") @ApiOperation(value = "添加用户接口",notes = "入参是复杂对象", produces = "application/json") public CommonResults addUser(@RequestBody UserEntity user) { // 调用service保持用户 return CommonResults.ok(); } }
3.4 启动springboot应用
4. API文档查看
打开地址:http://localhost:8888/doc.html
边系鞋带边思考人生.