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.mzph.cn/news/809097.shtml

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

相关文章

Flask、Django和Tornado怎么选

Flask、Django和Tornado是三个常用的PythonWeb框架&#xff0c;它们在设计理念、功能和适用场景上有所不同。下面是它们的对比&#xff1a; 1. 设计理念&#xff1a; - Flask是一个轻量级的框架&#xff0c;它提供了最基本的功能&#xff0c;但是具有高度的灵活性和可扩展性。…

Linux中的 mount -a

mount -a 是一个在 Linux 系统中用来挂载所有在 /etc/fstab 文件中定义的文件系统的命令。在 Linux 中&#xff0c;/etc/fstab 文件包含了系统启动时需要挂载的文件系统的信息&#xff0c;mount -a 命令会根据这些信息自动挂载这些文件系统。 mount -a 命令的作用&#xff1a;…

https加载http不安全脚本提示解决方案

大家好&#xff0c;我是咕噜铁蛋。今天&#xff0c;我想和大家探讨一个很常见但又很容易被忽视的问题——https加载http不安全脚本提示。相信很多网站开发者和维护者在日常工作中都遇到过这样的问题&#xff0c;那么我们应该如何解决这个问题呢&#xff1f;下面&#xff0c;我将…

将Ubuntu18.04默认的python3.6升级到python3.8

1、查看现有的 python3 版本 python3 --version 2、安装 python3.8 sudo apt install python3.8 3、将 python3.6 和 3.8 添加到 update-alternatives sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1 sudo update-alternatives --insta…

Golang编译优化——消除Copy指令

一、优化概述 以下是Go编译器对某个代码段编译生成的SSA IR摘选&#xff0c;对于Golang SSA IR的介绍我写了文章&#xff0c;但是在犹豫要不要发。 b1:-... Plain → b2 (5)b2: ← b1 b4-v9 (5) Phi <int> v8 v16 (i[int])v22 (8) Phi <int> v7 v14 (r[int])v1…

算法设计与分析实验报告c++python实现(生命游戏、带锁的门、三壶谜题、串匹配问题、交替放置的碟子)

一、实验目的 1&#xff0e;加深学生对分治法算法设计方法的基本思想、基本步骤、基本方法的理解与掌握&#xff1b; 2&#xff0e;提高学生利用课堂所学知识解决实际问题的能力&#xff1b; 3&#xff0e;提高学生综合应用所学知识解决实际问题的能力。 二、实验任务 1、 编…

减少服务器被入侵

减少服务器被入侵 排查方向 1、日志 查看/var/log下的日志&#xff0c;如果发现有大量SSH登录失败日志&#xff0c;并存在root用户多次登录失败后成功登录的记录&#xff0c;这就符合暴力P解特征 2、系统分析 对系统关键配置、账号、历史记录等进行排查&#xff0c;确认对系统…

全面学习SpringCloud框架指南

要深入学习Spring Cloud框架,你需要系统地掌握其核心组件和概念,并了解如何在实际项目中应用这些知识。以下是一些关键的学习点和相应的学习内容: 一共分为10个模块包括: 1、微服务架构基础: 理解微服务架构的概念和优势。 学习单体架构向微服务架构演进的过程。 掌握…

git常用命令整理~

在github创建仓库后的操作 git init git add . git commit -m 自定义 git remote add origin 仓库地址 git push -u origin master //第一次pushgit协作时用到的命令 git pull //push前先拉取别人的代码 git pushgit remote rm origin # 删除连接仓库git config --global use…

计算机网络 Telnet远程访问交换机和Console终端连接交换机

一、实验要求和内容 1、配置交换机进入特权模式密文密码为“abcd两位班内学号”&#xff0c;远程登陆密码为“123456” 2、验证PC0通过远程登陆到交换机上&#xff0c;看是否可以进去特权模式 二、实验步骤 1、将一台还没配置的新交换机&#xff0c;利用console线连接设备的…

LiveData和ViewModel源码学习

文章目录 LiveDataObserverWrapper 数据观察者LifecycleEventObserver 生命周期观察者 ViewModelViewModelStoreViewModelProviderViewModelStoreOwnerComponentActivityNonConfigurationInstancesActivity attach 恢复NonConfigurationInstancesActivityThread performDestroy…

在Java中实现记录1000万用户连续7天登录的功能,可以使用Redis的Bitmap来跟踪每个用户的登录状态

在Java中实现记录1000万用户连续7天登录的功能&#xff0c;可以使用Redis的Bitmap来跟踪每个用户的登录状态。以下是一个简化的Java示例&#xff0c;使用了Jedis库作为Redis的Java客户端。 首先&#xff0c;确保你已经在项目中添加了Jedis的依赖。如果你使用Maven&#xff0c;…

【高端电流检测IC储能产品应用方案】耐压28V侧轨的电流检测芯片FP130A 应用于电脑电源,开关电源以及多口快充充电器,户外移动电源,适配器,电池充电器等

电流检测技术常用于高压短路保护、电机控制、DC/DC换流器、系统功耗管理、二次电池的电流管理、蓄电池管理等电流侦测等场景。对于大多数应用而言&#xff0c;都是间接测量电阻两端的跨压差来获取待测电流。 如下面的高端电流检测芯片FP130A&#xff0c;丝印是FC915。电路原理图…

MySQL数据库的详解(1)

DDL&#xff08;数据库操作&#xff09; 查询 查询所有数据库&#xff1a;show databases;当前数据库&#xff1a;select database(); 创建 创建数据库&#xff1a;create database [ if not exists] 数据库名 ; 使用 使用数据库&#xff1a;use 数据库名 ; 删除 删除数…

利用Python实现可视化交互界面:Dash

Dash是一个低代码数据框架&#xff0c;用Python实现可视化交互界面&#xff0c;不用写Javascript&#xff0c;开源&#xff0c;支持回调、HTML组件等功能。 安装 pip install dash使用 # Import packages from dash import Dash, html, dash_table, dcc, callback, Output, …

网络安全JavaSE第六天

7. 数组 7.3.5 数组排序 7.3.5.1 冒泡排序 冒泡排序的思路&#xff1a;相邻两个元素进行比较&#xff0c;如果前面元素比后面元素大就交换位置&#xff0c;每一趟执行完后&#xff0c; 就会得到最大的数。 排序过程分析&#xff1a; package com.openlab; /** * 冒泡排序 */…

Gin 相对 标准库 net/http的优势

这些优势主要体现在以下几个方面&#xff1a; 简洁的路由分组和中间件支持 Gin允许开发者使用简洁的API来定义路由&#xff0c;支持路由分组和中间件&#xff0c;这使得构建具有复杂路由规则的大型应用变得更加简单和高效。 参数化路由 Gin支持参数化路由&#xff0c;可以很容…

pyside6自定义部件库和软件框架的建设记录

一、自定义部件库 原则上尽量做到前后端分离&#xff0c;接口方便&#xff0c;复制简单。 1、单选框部件 # encoding: utf-8 ################################################### # 自定义的单选框 #################################################### 对外…

wsl2 arch linux访问ch340串口和usb设备

wsl2 arch linux访问ch340串口和usb设备 安装usb转网络软件usbipd共享usb到网络wsl2安装usbip挂载USB设备卸载USB设备 安装usb转网络软件usbipd 在powershell执行下面命令安装 winget install --interactive --exact dorssel.usbipd-win共享usb到网络 #查看USB设备 usbipd l…

基于模型预测算法的含储能微网双层能量管理模型

基于模型预测算法的含储能微网双层能量管理模型 文章目录 基于模型预测算法的含储能微网双层能量管理模型一、项目介绍二、源程序下载 一、项目介绍 代码主要做的是一个微网双层优化调度模型&#xff0c;微网聚合单元包括风电、光伏、储能以及超级电容器&#xff0c;在微网的运…