You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot6中配置Swagger避免调用MessageSource解析API描述

解决方案:关闭Swagger的消息解析行为

方法1:通过配置文件直接禁用消息解析

在application.yml或application.properties中添加以下配置,直接关闭springdoc对接口描述的消息源解析逻辑:

application.yml

springdoc:
  # 禁用接口操作的消息解析
  resolve-operation-properties: false
  # 禁用Schema属性的消息解析(若有需要)
  resolve-schema-properties: false

application.properties

springdoc.resolve-operation-properties=false
springdoc.resolve-schema-properties=false

这两个配置项会让Swagger直接使用OpenAPI定义文件中硬编码的description文本,不再将其当作消息码调用MessageSource.resolveCode()。

方法2:自定义OpenApi配置类强制保留原始描述

如果配置文件方式不生效,可以创建一个配置类,手动遍历所有接口操作,强制保留原始描述:

import io.swagger.v3.oas.models.OpenAPI;
import org.springdoc.core.customizers.OpenApiCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SwaggerCustomConfig {

    @Bean
    public OpenApiCustomizer keepOriginalDescriptions() {
        return openApi -> {
            // 遍历所有接口路径
            openApi.getPaths().values().forEach(pathItem -> {
                // 处理每个路径下的所有操作(GET/POST等)
                pathItem.readOperations().forEach(operation -> {
                    // 强制保留原始接口描述
                    String originalDesc = operation.getDescription();
                    if (originalDesc != null) {
                        operation.setDescription(originalDesc);
                    }
                    // 处理参数描述
                    operation.getParameters().forEach(parameter -> {
                        String paramDesc = parameter.getDescription();
                        if (paramDesc != null) {
                            parameter.setDescription(paramDesc);
                        }
                    });
                    // 处理响应描述
                    operation.getResponses().values().forEach(response -> {
                        String respDesc = response.getDescription();
                        if (respDesc != null) {
                            response.setDescription(respDesc);
                        }
                    });
                });
            });
        };
    }
}

验证

启动SpringBoot应用后,访问Swagger UI页面,查看接口描述是否直接显示registration.yml中硬编码的文本(如'Register a user')即可确认配置生效。

内容的提问来源于stack exchange,提问作者ka3ak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 11:42:39