springboot 整合swagger

embedded/2024/9/24 0:29:37/

没有多余废话,就是干

spring-boot 2.7.8

springfox-boot-starter 3.0.0

结构

POM.xml

java"><?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.7.8</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo3</artifactId><version>0.0.1-SNAPSHOT</version><name>demo3</name><description>demo3</description><url/><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter --><dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>

application.xml

java">#spring:
#  application:
#    name: demo3
spring:mvc:pathmatch:matching-strategy: ant_path_matcher
server:port: 8081
# 配置springdoc-openapi,用于文档化和访问API
springdoc:# 配置Swagger UI的访问路径和排序方式swagger-ui:path: /swagger-ui.html  # Swagger UI的访问路径tags-sorter: alpha      # 按字母顺序排序标签operations-sorter: alpha  # 按字母顺序排序操作# 配置API文档的访问路径api-docs:path: /v3/api-docs  # API文档的访问路径# 配置API分组,用于组织和管理APIgroup-configs:- group: 'default'   # API分组名称paths-to-match: '/**'  # 匹配所有路径packages-to-scan: com.ykx.easyexceldemo02.controller  # 扫描的包,用于自动发现API

SwaggerConfig.java

java">package com.example.demo3.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.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration
@EnableOpenApi
public class SwaggerConfig {@Beanpublic Docket api() {return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.basePackage("com.example.demo3.web"))   // 替换为您的Controller所在的包路径.paths(PathSelectors.any()).build().apiInfo(apiInfo());}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("后端接口文档")    // 文档标题.description("前后端交互接口") // 文档路径.version("1.0.0")   // 文档版本.build();}}

Controller.java

java">package com.example.demo3.web;import com.example.demo3.entity.User;
import io.swagger.annotations.*;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;import javax.servlet.http.HttpServletRequest;@Api(tags = "控制类", hidden = true)
@RestController
@RequestMapping("/web")
public class Controller {@ApiOperation(value = "测试")@GetMapping("/come")public String hello(@ApiParam(value = "主键id") String id) {return "hello";}@ApiOperation("测试1")@GetMapping("/come1")@ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键id"),@ApiImplicitParam(name = "name", value = "用户名")})public String hello1(String id, String name, @RequestBody @ApiParam(value = "用户对象") User user) {return "hello";}@ApiOperation("测试2")@GetMapping("/come2")public String hello2(@ApiParam(value = "注解id") String id) {return "hello";}@ApiIgnore("忽略测试")@ApiOperation("测试1")@GetMapping("/come10")public String hello10(@ApiParam(value = "请求对象") HttpServletRequest request) {return "hello";}
}

User.java

java">package com.example.demo3.entity;import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;@ApiModel(value = "UserModel", description = "用户模型")
@Data
public class User {@ApiModelProperty(value = "主键id", required = true, example = "1", hidden = true, dataType = "int", position = 1, allowableValues = "1,2,3")private int id;@ApiModelProperty(value = "用户名", required = true, example = "zhangsan", position = 2)private String name;@ApiModelProperty(value = "年龄", required = true, example = "18", position = 3)private int age;
}

效果图展示:


http://www.ppmy.cn/embedded/111333.html

相关文章

【Redis入门到精通一】什么是Redis?

目录 Redis 1. Redis的背景知识 2.Redis特性 3.Redis的使用场景 4.Ubuntu上安装配置Redis Redis Redis在当今编程技术中的地位可以说非常重要&#xff0c;大多数互联网公司内部都在使用这个技术&#xff0c;熟练使用Redis已经成为开发人员的一个必备技能。本章将带领读者…

Newtonsoft.Json (Json.NET)使用笔记

Newtonsoft.Json 简单介绍许可证功能特点 代码示例基本类型的序列化和反序列化对象与集合的序列化和反序列化对象的序列化与反序列化集合的序列化和反序列化 自定义转换器的使用自定义日期格式自定义转换器处理特殊类型 动态类型和 LINQ to JSON使用动态类型解析未知结构的 JSO…

QGis二次开发 —— 1、Windows10搭建Vs2017-QGis环境(附Vs2017环境效果)(附:Qt助手加入QGis接口说明文档)

OSGeo4W简介 更高级的 QGIS 用户应该使用 OSGeo4W 包。此安装程序可以并行安装多个版本的 QGIS&#xff0c;并且还可以进行更高效的更新&#xff0c;因为每个新版本仅下载和安装更改的组件。      OSGeo4W 存储库包含许多来自 OSGeo 项目的软件。包括 QGIS 和所有依赖项&a…

uniapp对tabbar封装,简单好用

第一种&#xff0c;效果展示 上代码&#xff0c;新建一个公用组件&#xff0c;tabbar.vue <template><view class"tabbar"><view class"tabbar-item" click"tabbarbtn(0)"><image class"item-image" v-if"…

什么是 TDengine?

TDengine 是一款专为物联网、工业互联网等场景设计并优化的大数据平台&#xff0c;其核心模块是高性能、集群开源、云原生、极简的时序数据库。它能安全高效地将大量设备、数据采集器每天产生的高达 TB 甚至 PB 级的数据进行汇聚、存储、分析和分发&#xff0c;对业务运行状态进…

嵌入式软件--51单片机 DAY 3

一、独立按键 按键的作用相当于一个开关&#xff0c;按下时接通&#xff08;或断开&#xff09;&#xff0c;松开后断开&#xff08;或接通&#xff09;。 &#xff08;1&#xff09;需求 通过SW1、SW2、SW3、SW4四个独立按键分别控制LED1、LED2、LED3、LED4的亮灭&#xff0…

ETCD的备份和恢复

一、引言 ETCD是一个高度可用的键值存储系统&#xff0c;被广泛应用于Kubernetes等分布式系统中以存储关键配置数据和服务发现信息。由于ETCD的重要性&#xff0c;确保其数据的安全性和可靠性至关重要。本文将介绍ETCD备份与恢复的基础知识、常用方法及最佳实践。 二、概述 …

力扣9.7

115.不同的子序列 题目 给你两个字符串 s 和 t &#xff0c;统计并返回在 s 的 子序列 中 t 出现的个数&#xff0c;结果需要对 109 7 取模。 数据范围 1 < s.length, t.length < 1000s 和 t 由英文字母组成 分析 令dp[i][j]为s的前i个字符构成的子序列中为t的前j…