Spring Boot 框架集成Knife4j

news2024/10/7 10:15:51

本次示例使用 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 接口, 代码示例如下:


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
}

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

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

界面效果图如下:

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

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

相关文章

看现货白银平台测评要注意的几个事项

在网上我们会看到很多现货白银平台测评的文章或短视频&#xff0c;我们要注意&#xff0c;这些测评内容包括本文在内&#xff0c;多少都会存在着一些主观性的东西&#xff0c;这是无可避免的。不过在看现货白银平台测评时&#xff0c;有一些客观的东西&#xff0c;是需要我们留…

专注底层技术创新,超高性能公链新星 Sui Network 有何独特优势?

近年来&#xff0c;Sui Network 为了能够打造让开发者低成本实现广泛应用开发的公链环境付诸实际行动。其建立了以对象为中心的数据模型、在交易签名和 PTB 中实现精细化权限、优化用户友好功能&#xff0c;逐步为开发者和用户提供了一个更为灵活、安全的链上运行环境。在优越技…

【吊打面试官系列】Java高并发篇 -为什么使用 Executor 框架比使用应用创建和管理线程好?

大家好&#xff0c;我是锋哥。今天分享关于 【为什么使用 Executor 框架比使用应用创建和管理线程好&#xff1f;】面试题&#xff0c;希望对大家有帮助&#xff1b; 为什么使用 Executor 框架比使用应用创建和管理线程好&#xff1f; 为什么要使用 Executor 线程池框架 1、每…

springboot 载入自定义的yml文件转DTO

改进方法&#xff0c;直接spring注入 import cn.hutool.json.JSONUtil; import org.springframework.beans.factory.config.YamlMapFactoryBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import …

开抖音小店需要用到哪些软件?全部都给大家整理好了,快来看看!

哈喽~我是电商月月 在抖音小店的运营过程中&#xff0c;选品上架&#xff0c;售后客服都是要靠软件辅助进行的 那开抖音小店到底会用到哪些软件&#xff1f;这些平台都叫什么&#xff1f; 今天我就给大家介绍一下抖音小店运营过程中会使用到的软件&#xff0c;干货满满&…

图片/视频上传(超简单教程)

#应用场景# 该后端开发接口适用 图片/视频上传&#xff0c;返回路径名称场景 1.视频上传 写在Controller层 这里只是一个接收&#xff0c;调用uploadObject方法上传oss public OmsResult<FileUploadDto> goodsUploadVideo(RequestParam(value "file") Mu…

SQL优化——执行计划

文章目录 1、获取执行计划常用方法1.1、使用AUTOTRACE查看执行计划1.2、使用EXPLAIN PLAN FOR查看执行计划1.3、查看带有A-TIME的执行计划1.4、查看正在执行的SQL的执行计划 2、定制执行计划3、怎么通过查看执行计划建立索引4、运用光标移动大法阅读执行计划 SQL执行缓慢有很多…

ubuntu环境下使用g++把c++编译成汇编语言(暂时)

1. 引言 为了深入理解c&#xff0c;决定学习一些简单的汇编语言。使用ubuntu系统下g很容易将一个c的文件编译成汇编语言。本文使用此方法&#xff0c;对一个简单的c文件编译成汇编语言进行理解。 2.示例 文件名&#xff1a;reorder_demo.cpp #include<stdio.h>typede…

【网络运维知识】—路由器与交换机区别

【网络运维知识】—路由器与交换机区别 一、路由器&#xff08;Router&#xff09;和交换机&#xff08;Switch&#xff09;对比1.1 功能1.2 转发方式1.3 范围1.4 处理方式 &#x1f496;The Begin&#x1f496;点点关注&#xff0c;收藏不迷路&#x1f496; 路由器&#xff08…

MySQL8.0 一主二从

1. 系统环境 cat /etc/redhat-release CentOS Linux release 7.9.2009 (Core)192.168.183.137 mysql-master 192.168.183.153 mysql-slave-1 192.168.183.154 mysql-slave-2# 关闭SELINUX sed -i s/SELINUXenforcing/SELINUXdisabled/g /etc/selinux/config seten…

基于SpringBoot的幼儿园管理系统 免费获取源码

项目源码获取方式放在文章末尾处 项目技术 数据库&#xff1a;Mysql5.7 数据表&#xff1a;16张 开发语言&#xff1a;Java(jdk1.8) 开发工具&#xff1a;idea 前端技术&#xff1a;html 后端技术&#xff1a;SpringBoot 功能简介 项目获取关键字&#xff1a;幼儿园 该…

React-css-in-js技术

​&#x1f308;个人主页&#xff1a;前端青山 &#x1f525;系列专栏&#xff1a;React篇 &#x1f516;人终将被年少不可得之物困其一生 依旧青山,本期给大家带来React篇专栏内容:React-css-in-js技术 目录 1、简介 2、定义样式与使用 3、样式继承 4、属性传递 1、简介 …

买婴儿洗衣机怎么选择?四大绝佳好用婴儿洗衣机分享

幼龄时期的宝宝的衣物&#xff0c;是比较需要注意的时候。可能一不注意宝宝穿在身上就会有不适宜症状发生。所以宝妈们真的要随时观察&#xff0c;然后在宝宝洗衣服的这上面多下点功夫&#xff0c;不要让宝宝受到这种无谓的伤害。小婴儿的抵抗力比我们差很多。有些细菌、病毒可…

IP地址怎么实现https

IP可以申请SSL证书。可以解决企业需要对IP实现https加密的需求&#xff0c;一张证书可以支持同时绑定多个IP。 IP证书有两种级别&#xff1a;基础级IP SSL证书和标准企业级IP SSL证书。 基础型SSL证书只需要10-30分钟即可颁发&#xff0c;企业型需要1-3个工作日即可颁发。 企…

Suno,属于音乐的ChatGPT时刻来临

AI绘画 AI视频我们见过了&#xff0c;现如今AI都能生成一首音乐&#xff0c;包括编曲&#xff0c;演唱&#xff0c;而且仅需几秒的时间便可创作出两分钟的完整歌曲 相信关注苏音的很大一部分都是从获取编曲或者混音插件来的&#xff0c;现如今AI却能帮你几秒生成曲子 今天就带…

deepspeed笔记

文章目录 一、deepspeed是什么&#xff1f;二、能训多大的模型&#xff0c;耗时如何&#xff1f;三、RLHF训练流程四、通信策略 一、deepspeed是什么&#xff1f; 传统的深度学习&#xff0c;模型训练并行&#xff0c;是将模型参数复制多份到多张GPU上&#xff0c;只将数据拆分…

C语言结课实战项目_贪吃蛇小游戏

目录 最终实现效果&#xff1a; 实现基本的功能&#xff1a; 根据游戏进程解释代码&#xff1a; 游戏初始化&#xff1a; 首先进入游戏&#xff0c;我们应该将窗口名称改为 “贪吃蛇” 并将光标隐藏掉。再在中间打印游戏信息。 之后我们要把地图打印出来&#xff1a; 然后…

数据可视化插件echarts【前端】

数据可视化插件echarts【前端】 前言版权开源推荐数据可视化插件echarts一、如何使用1.1 下载1.2 找到js文件1.3 入门使用1.4 我的使用 二、前后端交互&#xff1a;入门demo2.1 前端htmljs 2.2 后端entitycontrollerservicemapper 三、前后端交互&#xff1a;动态数据3.1 前端j…

书生·浦语大模型全链路开源体系-第6课

书生浦语大模型全链路开源体系-第6课 书生浦语大模型全链路开源体系-第6课相关资源Lagent & AgentLego 智能体应用搭建环境准备创建虚拟环境安装LMDeploy安装 Lagent安装 AgentLego Lagent 轻量级智能体框架使用 LMDeploy 部署启动并使用 Lagent Web Demo使用自定义工具获取…

mysql的mgr集群的网络不可达之后脑裂的问题

此时主节点上的dml和ddl操作都会挂死&#xff0c;由于脑裂问题&#xff0c;无法判断谁是主谁是备&#xff0c;所以节点无法写操作。 此时需要手动介入处理&#xff1a; mysql> show variables like %group_replication_member_expel_timeout%; ---------------------------…