概述
Knife4j官文:https://doc.xiaominfo.com/knife4j/documentation/
1.什么是Knife4j?
knife4j是为Java MVC框架集成Swagger生成Api文档的增强解决方案,前身是swagger-bootstrap-ui,取名
kni4j是希望她能像一把匕首一样小巧,轻量,并且功能强悍!
ps:太懒了,直接baidu复制的,hiahia
话不多说,直接上Demo
2.首先肯定是引入依赖啦
<knife4j.version>3.0.2</knife4j.version> -->使用同一版本管理的看这里
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>${knife4j.version}</version>
</dependency>
3.增加配置文件Knife4jConfig
package com.uwooz.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;
/**
* Knife4j配置文件
**/
@Configuration
public class Knife4jConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.demo.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("标题")
.version("1.0.0")
.description("介绍")
.build();
}
}
4.具体使用方式与Swagger无太大差别
4.1 实体类
package com.uwooz.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.uwooz.entity.enums.AdminStateEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* admin表
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value = "Admin对象", description = "")
public class Admin implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "管理员表id")
@TableId(type = IdType.ASSIGN_UUID)
private String id;
@ApiModelProperty(value = "用户名")
private String adminName;
@ApiModelProperty(value = "密码")
private String password;
}
4.2 业务代码 (本代码只做Knife4j演示作用,不涉及具体逻辑)
package com.uwooz.controller;
import com.uwooz.common.ApiResult;
import com.uwooz.common.Result;
import com.uwooz.entity.Admin;
import com.uwooz.pojo.dto.admin.AddAdminDTO;
import com.uwooz.pojo.dto.admin.ModifyAdminDTO;
import com.uwooz.service.AdminService;
import com.uwooz.utils.ApiResultUtil;
import com.uwooz.utils.PageBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
/**
* 管理员
*/
@RestController
@Api(tags = "管理员管理")
@RequestMapping("/admin")
public class AdminController {
@Autowired
private AdminService adminService;
@PostMapping("/save")
@ApiOperation(value = "新增管理员")
public Void save(@Valid @RequestBody Admin admin) {
//本代码只做Knife4j演示作用,不涉及业务
adminService.addAdmin(admin);
}
@GetMapping("/list")
@ApiOperation(value = "跟据名称模糊查询管理员列表")
@ApiImplicitParams({
@ApiImplicitParam(name = "adminName", value = "管理员名称", dataType = "string"),
})
public List<Admin> listByName(String adminName) {
return adminService.listByName(adminName);
}
}
5.访问方式(自行修改ip和端口)
-
初始Swagger的访问方式为:http://localhost:1000/swagger-ui.html
-
Knife4j的访问方式为:http://localhost:1000/doc.html
PS:以上就是所有内容啦,奥里给!
最后
以上就是唠叨悟空为你收集整理的生成Api文档不再发愁,Swagger升级版Knife4j来啦的全部内容,希望文章能够帮你解决生成Api文档不再发愁,Swagger升级版Knife4j来啦所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复