Spring Boot 框架集成Knife4j

本次示例使用 Spring Boot 作为脚手架来快速集成 Knife4j,Spring Boot 版本2.3.5.RELEASE,Knife4j 版本2.0.7,完整代码可以去参考 knife4j-spring-boot-fast-demo

pom.xml 完整文件代码如下

<?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.3.5.RELEASE</version><relativePath/> </parent><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-fast-demo</artifactId><version>1.0</version><name>knife4j-spring-boot-fast-demo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>2.0.9</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

第一步:在 maven 项目的pom.xml中引入 Knife4j 的依赖包,代码如下:

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

第二步:创建 Swagger 配置依赖,代码如下:

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.EnableSwagger2WebMvc;@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfiguration {@Bean(value = "defaultApi2")public Docket defaultApi2() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(getInfo())//分组名称.groupName("2.X版本").select()//这里指定Controller扫描包路径.apis(RequestHandlerSelectors.basePackage("com.test.controller")).paths(PathSelectors.any()).build();}private static ApiInfo getInfo() {return new ApiInfoBuilder().title("xxxxx软件系统").description("# xxxx是基于 xx平台的新一代 软件系统").termsOfServiceUrl("http://www.test.com/").contact(new Contact("mabh","http://www.test.com","test@test.com")).version("1.0").build();}
}

RequestHandlerSelectors.basePackage 要改成你自己的。

IndexController.java包含一个简单的 RESTful 接口, 代码示例如下:


此时,启动 Spring Boot 工程,在浏览器中访问:http://localhost:8080/doc.html


import com.test.TabaseWebDemo.Sex;
import com.test.TabaseWebDemo.UserModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;import java.util.Arrays;
import java.util.List;@Api(tags = "首页模块")
@RestController
public class IndexController {@ApiImplicitParam(name = "name",value = "姓名",required = true)@ApiOperation(value = "向客人问好")@GetMapping(value = "/sayHi",produces = MediaType.APPLICATION_JSON_VALUE)public ResponseEntity<String> sayHi(@RequestParam(value = "name") String name){return ResponseEntity.ok("Hi:"+name);}@GetMapping("/user")@ApiOperation("获取用户信息接口")public String getUser(@ApiParam(value = "用户ID", required = true) @RequestParam("id") String userId) {// 根据用户ID获取用户信息return "用户信息:" + userId;}@PostMapping("/user")@ApiOperation("创建用户接口")@ApiImplicitParam(name = "user", value = "用户对象", required = true, dataType = "User")public String createUser(@RequestBody UserModel user) {// 处理用户创建逻辑return "用户创建成功!";}// 获取所有@GetMapping(value = "/users",produces = MediaType.APPLICATION_JSON_VALUE)@ApiOperation("获取所有用户接口")public List<UserModel> getAllUsers() {// 处理获取所有用户逻辑return Arrays.asList(new UserModel("张三", Sex.man,18),new UserModel("李四", Sex.woman,20),new UserModel("王五", Sex.man,22),new UserModel("赵六", Sex.woman,24));}@ApiIgnore@GetMapping("/ignore")public String ignore() {return "这个接口被忽略";}}

import io.swagger.annotations.ApiModelProperty;public class UserModel {@ApiModelProperty(value = "用户名", required = true)private String username;@ApiModelProperty(value = "性别", required = true)private Sex sex;@ApiModelProperty(value = "年龄", required = true)private int age;public UserModel() {}public UserModel(String username, Sex sex, int age) {this.username = username;this.sex = sex;this.age = age;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public Sex getSex() {return sex;}public void setSex(Sex sex) {this.sex = sex;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
public enum Sex {man,woman
}

更多注解使用方法:
https://github.com/swagger-api/swagger-core/wiki/Annotations

界面效果图如下:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/610522.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2024.4.12蚂蚁庄园今日答案:豆腐在烹调时容易碎有什么办法可以避免?

原文来源&#xff1a;蚂蚁庄园今日答案 - 词令 蚂蚁庄园是一款爱心公益游戏&#xff0c;用户可以通过喂养小鸡&#xff0c;产生鸡蛋&#xff0c;并通过捐赠鸡蛋参与公益项目。用户每日完成答题就可以领取鸡饲料&#xff0c;使用鸡饲料喂鸡之后&#xff0c;会可以获得鸡蛋&…

QT学习day5

#include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget),socket(new QTcpSocket(this)) {ui->setupUi(this);//初始化界面ui->msgEdit->setEnabled(false);//不可用ui->sendBtn-&g…

Java File类

2. File类 2.1 概述 java.io.File 类是文件和目录路径名的抽象表示&#xff0c;主要用于文件和目录的创建、查找和删除等操作。 2.2 构造方法 public File(String pathname) &#xff1a;通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例。 public File(String …

【前端】JavaScript(概念+语法+形式+变量+数组+函数+作用域+对象)

文章目录 JavaScript一、JavsScript概念1.JavaScript的开发方向2.JavaScript和CSS、HTML的关系3.JavaScript运行过程4.JavaScript的组成 二、JavaScript的语法1.JS的书写形式1.行内式2.内嵌式3.外部式4.注释5.输入输出1.prompt和alert2.输出: console.log 2.变量的使用1.创建变…

对 FileReader 的理解

1、文档 FileReader - Web API 接口参考 | MDN 2、概念 FileReader 对象是一个内置的 JavaScript 对象&#xff0c;用于在客户端&#xff08;浏览器&#xff09;中异步读取文件内容。 它提供了一种在 Web 应用程序中读取文件数据的方式&#xff0c;可以读取文件内容并将其转…

C/C++基础----运算符

算数运算符 运算符 描述 例子 两个数字相加 两个变量a b得到两个变量之和 - 两个数字相减 - * 两个数字相乘 - / 两个数字相除 - % 两个数字相除后取余数 8 % 3 2 -- 一个数字递减 变量a&#xff1a;a-- 、--a 一个数字递增 变量a: a 、 a 其中递…

Java二叉树(2)

一、二叉树的链式存储 二叉树的存储分为顺序存储和链式存储 &#xff08;本文主要讲解链式存储&#xff09; 二叉树的链式存储是通过一个一个节点引用起来的&#xff0c;常见的表示方式有二叉三叉 // 孩子表示法 class Node { int val; // 数据域 Node left; // 左孩子的引用…

【VS2019】x64 Native Tools Command Prompt for Vs 2019使用conda命令进入环境

【VS2019】x64 Native Tools Command Prompt for Vs 2019使用conda命令进入环境 安装完VS2019后&#xff0c;打开终端x64 Native Tools Command Prompt for Vs 2019&#xff0c;直接运行conda会出现‘conda’ 不是内部或外部命令&#xff0c;也不是可运行的程序 原因分析&am…

vite 和 rollup

Rollup Rollup 是一个 JavaScript 模块打包器&#xff0c;它可以将多个模块打包成一个单独的文件。 rollup 的特点&#xff1a; 不会生成过多的运行代码 可以多模块化规范打包 input&#xff08;输入&#xff09;: 这是你的项目的入口点&#xff08;即主要的 JavaScript 文…

[C++][算法基础]树的重心(树图DFS)

给定一颗树&#xff0c;树中包含 n 个结点&#xff08;编号 1∼n&#xff09;和 n−1 条无向边。 请你找到树的重心&#xff0c;并输出将重心删除后&#xff0c;剩余各个连通块中点数的最大值。 重心定义&#xff1a;重心是指树中的一个结点&#xff0c;如果将这个点删除后&a…

考研数学|零基础冲130复习方案+资料分享

如果想考130&#xff0c;那就一定要好好复习基础。 因为24年开始&#xff0c;考研的走向就已经发生了改变&#xff0c;考研数学开始越来越注重一下三点的考察&#xff1a; 基础知识的掌握程度计算能力解题能力 这三点环环相扣&#xff0c;每一个都要复习好才行。 在基础阶段…

Linux操作系统的学习

Linux系统的目录结构 / 是所有目录的顶点目录结构像一颗倒挂的树 Linux常用命令 常见命令 序号命令对应英文作用1lslist查看当前目录下的内容2pwdprint work directory查看当前所在目录3cd [目录名]change directory切换目录4touch [文件名]touch如果文件不存在&#xff0c;新…