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

SpringBoot整合Swagger3

前言

swagger是啥,是干什么的,有什么用,我想在这里我就不用介绍了,下面直接代码演示。

添加依赖

<?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><groupId>com.inchlifc</groupId><artifactId>SwaggerDemo</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>SwaggerDemo</name><description>Demo project for Spring Boot</description><properties><java.version>8</java.version></properties><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.9</version></parent><dependencies><dependency><groupId>io.springfox</groupId><artifactId>springfox-spring-web</artifactId><version>3.0.0</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>3.1.3</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><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.1</version></plugin></plugins></build></project>

 添加配置

package com.inchlifc.swaggerdemo.config;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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;/*** Swagger3的接口配置* * @author chery*/
@Configuration
public class SwaggerConfig{// 控制是否允许swaggerprivate boolean enableSwagger = true;@Beanpublic Docket api() {return new Docket(DocumentationType.OAS_30) //展示的swagger文档格.enable(enableSwagger) //是否启动swagger配置.select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build();}}

配置文件

spring:mvc:pathmatch:matching-strategy: ant_path_matcher

测试Controller

package com.inchlifc.swaggerdemo.controller;import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/swagger")
@Api(value = "测试",tags = {"测试Swagger"})
public class TestController {@ApiOperation(value = "测试接口")@RequestMapping(value = "/hello",method = RequestMethod.GET)public String hello(){return "hello";}
}

启动演示

http://www.lryc.cn/news/160267.html

相关文章:

  • detectron2 install path
  • 如何将DHTMLX Suite集成到Scheduler Lightbox中?让项目管理更可控!
  • 什么是JVM常用调优策略?分别有哪些?
  • 《向量数据库指南》——向量数据库Milvus Cloud 2.3的可运维性:从理论到实践
  • select多选回显问题 (取巧~)
  • 光伏并网双向计量表ADL400
  • 十三、MySQL(DQL)语句执行顺序
  • 【高德地图】根据经纬度多边形的绘制(可绘制区域以及任意图形)
  • C++ std::pair and std::list \ std::array
  • C++的类型转换
  • 【Selenium2+python】自动化unittest生成测试报告
  • 【APISIX】W10安装APISIX
  • [Linux]动静态库
  • 2023高教社杯数学建模国赛C题思路解析+代码+论文
  • macos13 arm芯片(m2) 搭建hbase docker容器 并用flink通过自定义richSinkFunction写入数据到hbase
  • FLV封装格式
  • [NLP]LLM---FineTune自己的Llama2模型
  • git在linux情况下设置git 命令高亮
  • C++ 表驱动方法代替if-else
  • 2023国赛数学建模E题思路分析 - 黄河水沙监测数据分析
  • cadence后仿真/寄生参数提取/解决pin口提取不全的问题
  • Vue中实现3D得球自动旋转
  • 使用wkhtmltoimage实现生成长图分享
  • 新风机未来什么样?
  • python的几种数据类型的花样玩法(一)
  • python回调函数之获取jenkins构建结果
  • Docker底层实现
  • PY32F003F18之RS485通讯
  • 概率论与数理统计学习笔记(7)——全概率公式与贝叶斯公式
  • 深入了解 Axios 的 put 请求:使用技巧与最佳实践