当前位置: 首页 > news >正文

Springboot集成knife4j (swagger)

1、添加依赖

在pom.xml 文件中添加 knife4j-spring-boot-starter 的依赖

<dependency>  <groupId>com.github.xiaoymin</groupId>  <artifactId>knife4j-spring-boot-starter</artifactId>  <version>3.0.3</version>  
</dependency>

2、配置

# application.yml  
knife4j:  enable: true # 开启Knife4j增强功能  base-package: com.xxx.xxx.controller # 设置需要扫描的包路径

3、使用Swagger注解

为实体类添加@ApiModel注解和ApiModelProperty注解
例如:

@ApiModel("用户实体类")
@Data
@TableName("employee")
public class Employee implements Serializable {@ApiModelProperty("用户Id")@TableField(value = "id", select = false)@TableId(type = IdType.ASSIGN_ID)private Long id;@ApiModelProperty("用户名称")private String name;@ApiModelProperty("用户密码")private String password;@TableField(value = "create_time",fill = FieldFill.INSERT)@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")private LocalDateTime createTime;@TableField(value = "update_time",fill = FieldFill.INSERT_UPDATE)@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")private LocalDateTime updateTime;
}

在 Controller 类和方法上使用 Swagger 的注解来描述 API

@Api(tags = "示例接口")  
@RestController  
@RequestMapping("/example")  
public class ExampleController {  @ApiOperation("获取示例数据")  @GetMapping("/data")  public String getData() {  return "示例数据";  }  
}

4、定义API文档的标题、描述、版本等信息

创建配置类 SwaggerConfig :

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.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration
@EnableSwagger2
public class SwaggerConfig {@Beanpublic Docket api() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfo("你的API标题", // title"你的API描述", // description"1.0", // version"Terms of service", // termsOfServiceUrl"你的联系人名字", // contact"你的API许可证", // license"你的API许可证URL" // licenseUrl);}
}

5、启动并访问

启用springboot项目,在浏览器访问项目的/doc.html路径
在这里插入图片描述

相关文章:

  • 突破!AI机器人拥有嗅觉!仿生嗅觉芯片研究登上Nature子刊
  • STC89C51学习笔记(二)
  • Flutter-发布插件到pub上传不上问题
  • RedisDesktopManager 安装
  • 网络基础二——TCP可靠性实现机制补充
  • Composer常见错误及解决方法
  • 金融中的数学知识
  • 内部类(InnerClass) 总结
  • 计算机网络-从输入网址到访问网站的全过程
  • 金融数据_PySpark-3.0.3随机森林(RandomForestClassifier)实例
  • AI大模型与网球运动结合的应用场景及案例分析
  • 精品PPT-2023年无人驾驶汽车车联网网络安全方案
  • Unity开发者3D模型基础
  • Java中的try catch finally结构
  • MongoDB聚合运算符:$maxN
  • 2017前端实习生面试总结
  • iOS小技巧之UIImagePickerController实现头像选择
  • JavaScript 基础知识 - 入门篇(一)
  • Javascript基础之Array数组API
  • Service Worker
  • v-if和v-for连用出现的问题
  • 订阅Forge Viewer所有的事件
  • 双管齐下,VMware的容器新战略
  • 我们雇佣了一只大猴子...
  • #define、const、typedef的差别
  • #Linux(make工具和makefile文件以及makefile语法)
  • #Spring-boot高级
  • $ is not function   和JQUERY 命名 冲突的解说 Jquer问题 (
  • (1)(1.19) TeraRanger One/EVO测距仪
  • (10)ATF MMU转换表
  • (11)MSP430F5529 定时器B
  • (2015)JS ES6 必知的十个 特性
  • (6)设计一个TimeMap
  • (二) Windows 下 Sublime Text 3 安装离线插件 Anaconda
  • (七)MySQL是如何将LRU链表的使用性能优化到极致的?
  • (心得)获取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列。
  • (续)使用Django搭建一个完整的项目(Centos7+Nginx)
  • (一) springboot详细介绍
  • (转)程序员疫苗:代码注入
  • .NET 8 中引入新的 IHostedLifecycleService 接口 实现定时任务
  • .net core 控制台应用程序读取配置文件app.config
  • .net2005怎么读string形的xml,不是xml文件。
  • .NET框架类在ASP.NET中的使用(2) ——QA
  • .NET序列化 serializable,反序列化
  • .NET中两种OCR方式对比
  • @JSONField或@JsonProperty注解使用
  • @SuppressWarnings注解
  • [ C++ ] STL priority_queue(优先级队列)使用及其底层模拟实现,容器适配器,deque(双端队列)原理了解
  • [@Controller]4 详解@ModelAttribute
  • [20170728]oracle保留字.txt
  • [20180129]bash显示path环境变量.txt
  • [Android]Android P(9) WIFI学习笔记 - 扫描 (1)
  • [Angularjs]asp.net mvc+angularjs+web api单页应用之CRUD操作
  • [boost]使用boost::function和boost::bind产生的down机一例
  • [Bzoj4722]由乃(线段树好题)(倍增处理模数小快速幂)