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

spring boot 2.7集成旧的springfox-boot-starter swagger oas 3.0

旧版本目前已经不维护推荐使用 springdoc-openapi-ui,这里为了演示使用旧的最新依赖

<dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</version>
</dependency>

配置spring的类

package com.dxy.ops.conf;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
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;@Configuration
@PropertySource("classpath:swagger.properties")
public class SwaggerConfig {@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.dxy.ops.controller")).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("api dock test").description("desc").version("v1").build();}}

因为默认解析的域名在k8s环境下不一定能用,所以可以访问域名通过环境变量注入实现:

package com.dxy.ops.conf;import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.servers.Server;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import springfox.documentation.oas.web.OpenApiTransformationContext;
import springfox.documentation.oas.web.WebMvcOpenApiTransformationFilter;
import springfox.documentation.spi.DocumentationType;import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;@Component
public class SpringFoxSwaggerHostResolver implements WebMvcOpenApiTransformationFilter {@Value("${SW_DOMAIN:localhost}")private String swaggerHost;@Value("${SW_NAME:none}")private String desc;@Overridepublic OpenAPI transform(OpenApiTransformationContext<HttpServletRequest> context) {OpenAPI swagger = context.getSpecification();// 创建新的服务器对象Server server = new Server();// 设置您的自定义域名URL,可以包含协议、域名和基础路径server.setUrl(this.swaggerHost);server.setDescription(this.desc);// 替换服务器列表swagger.setServers(Arrays.asList(server));return swagger;}@Overridepublic boolean supports(DocumentationType documentationType) {// 仅对OpenAPI 3.0生效return documentationType.equals(DocumentationType.OAS_30);}
}

就可以修改下面的地方:

指定的:

http://www.xdnf.cn/news/8605.html

相关文章:

  • 论文阅读笔记——Emerging Properties in Unified Multimodal Pretraining
  • 超全GPT-4o 风格提示词案例,持续更新中,附使用方式
  • 行为型:迭代器模式
  • java面试题
  • 物联网代理暴利逻辑拆解:格行随身WiFi三网切换技术实战分析
  • 机器学习中的多GPU训练模式
  • 向量数据库Milvus03-高级功能与性能调优
  • 7:QT加载保存参数(读写日志)
  • JS逆向 - 狗dong参数Log及joyytokem(补环境)
  • Groovy:Java 的简洁版
  • python web 开发-Flask-Login使用详解
  • 构建安全AI风险识别大模型:CoT、训练集与Agent vs. Fine-Tuning对比
  • 每日leetcode(昨天赶飞机没做,今天补)
  • 深入理解 Redis 哨兵模式
  • H3C-W2000-G2【透明反代理】
  • Nacos适配GaussDB超详细部署流程
  • 【数据结构初阶】顺序表专题
  • 在Rockchip平台上利用FFmpeg实现硬件解码与缩放并导出Python接口
  • Selenium 测试框架 - Python
  • SpringCloud实战:使用Sentinel构建可靠的微服务熔断机制
  • 从 0 开始部署 Archivematica (windows环境)
  • RabbitMQ 概述
  • 【C/C++】多线程开发:wait、sleep、yield全解析
  • 国标GB28181设备管理软件EasyGBS打造厨房全方位实时监控解决方案
  • CC工具箱使用指南:【平行线两端闭合】
  • kali的简化安装
  • 水利水电安全员考试的案例分析题一般涉及哪些方面的知识?
  • 精明的猎人VS精明的狐狸
  • Python之os模块(文件和目录操作、进程管理、环境变量访问)
  • Firebase 之 归因