Swagger 修改默认配置

本文介绍了如何修改Swagger的默认配置,通过创建Docket并指定DocumentationType,自定义ApiInfo对象来个性化设置Swagger的作者、版本等信息。完成配置后,运行项目并在http://localhost:8080/swagger-ui.html查看效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

swagger配置

package com.sky.config;

import org.springframework.context.annotation.Configuration;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 使用swagger需要创建一个配置类,并开启swagger配置
 * ps:swagger和 swagger2 不能通用
 *
 * 步骤:
 * 1.将swaggerConfig注册到ioc容器中
 * 2.开启swagger2功能
 * 3.运行项目,访问swagger-ui.html
 */
@Configuration // 就是@Component
@EnableSwagger2 // 开启swagger
public class Swagger2Config {
   
}

那我们要怎么更改swagger的默认配置呢?
先看 @EnableSwagger2

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package springfox.documentation.swagger2.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;
import springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration;

@Retention(RetentionPolicy.RUNTIME)
@Target({
   ElementType.TYPE})
@Documented
@Import({
   Swagger2DocumentationConfiguration.class}) // 导入指定类
public @interface EnableSwagger2 {
   
}

该类的注解上有==@Import({Swagger2DocumentationConfiguration.class})==,导入了 Swagger2DocumentationConfiguration.class 这个类,继续看一下这个类是干哈的。

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package springfox.documentation.swagger2.configuration;

import ...

@Configuration // 该类是一个配置类
@Import({
   SpringfoxWebMvcConfiguration.class, SwaggerCommonConfiguration.class})
@ComponentScan(
    basePackages = {
   "springfox.documentation.swagger2.mappers"}
)
@ConditionalOnWebApplication
public class Swagger2DocumentationConfiguration {
   
    public Swagger2DocumentationConfiguration() {
   
    }

    @Bean
    public JacksonModuleRegistrar swagger2Module() {
   
        return new Swagger2JacksonModule();
    }

    @Bean
    public HandlerMapping swagger2ControllerMapping(Environment environment, DocumentationCache documentationCache, ServiceModelToSwagger2Mapper mapper, JsonSerializer jsonSerializer) {
   
        return new PropertySourcedRequestMappingHandlerMapping(environment, new
### 如何正确配置 Swagger 的访问路径及 URL 设置 Swagger 提供了一种简单的方法来生成 API 文档并提供交互式的界面。为了确保 Swagger 访问路径和实际接口一致,可以通过以下方式调整其基础 URL 和其他相关参数。 #### 方法一:通过 YAML 文件设置 Base URL 在项目的 `application.yml` 或 `application.properties` 中可以显式指定 Swagger 使用的基础 URL。这使得无论是在本地开发环境还是生产环境中,都可以统一管理 API 路径[^2]。 以下是基于 Spring Boot 的 YAML 配置示例: ```yaml springfox: documentation: swagger: v2: host: yourapp.abc.com/project-name ``` 上述配置将覆盖默认的主机名(如 `localhost` 或 `127.0.0.1`),从而让 Swagger UI 显示自定义的域名作为基础路径。 --- #### 方法二:修改 SwaggerConfig 配置类 如果项目中有多个版本或者存在冲突的操作方法,则可以在 Swagger配置类中进一步优化。例如,在初始化 Docket 对象时,可以手动设置 Host 属性或其他高级选项[^3]。 下面是 Java 配置代码的一个例子: ```java 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; @Configuration public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .host("yourapp.abc.com") // 自定义Host地址 .select() .apis(RequestHandlerSelectors.basePackage("com.example.controller")) .paths(PathSelectors.any()) .build(); } } ``` 此代码片段中的 `.host()` 方法允许开发者明确指出哪个域名应该被用作 API 请求的基础部分。 --- #### 方法三:处理操作名称冲突 当某些控制器方法具有相同的 HTTP 动词和路径模式时,可能会引发冲突警告。此时可通过如下逻辑解决此类问题: ```java @Bean public Docket customDocket(SwaggerPluginSupport defaultProvider, String xmlFile) { Docket docket = (new Docket(DocumentationType.SWAGGER_2)); docket.select().resolveConflictingActions(apiDescriptions -> apiDescriptions.get(0)) .customProvider((provider) -> new SwaggerHelpTools(provider, xmlFile)); return docket; } ``` 这里的关键在于调用了 `ResolveConflictingActions` 来决定保留哪一个重复项,并且还引入了一个扩展工具类来自定义行为。 --- ### 总结 以上三种方法分别适用于不同场景下的需求——从简单的全局替换到复杂的个性化定制均可实现。合理运用这些技巧可以帮助团队更高效地维护 RESTful 接口文档及其关联的服务端点。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值