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

Spring Boot中如何修改OpenAPI生成DTO的验证错误提示语言?

将Spring Boot + OpenAPI DTO的验证错误提示改为英语

1. 配置OpenAPI Generator生成英文友好的验证注解

OpenAPI生成DTO时,需确保验证注解使用国际化消息key而非硬编码俄语。通过构建插件(Maven/Gradle)指定语言和消息源:

Maven 插件配置示例

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>6.6.0</version> <!-- 使用最新稳定版本 -->
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/openapi.yaml</inputSpec>
                <generatorName>spring</generatorName>
                <configOptions>
                    <interfaceOnly>true</interfaceOnly> <!-- 按需调整 -->
                    <useSpringBoot3>true</useSpringBoot3> <!-- 适配你的Spring Boot版本 -->
                </configOptions>
                <additionalProperties>
                    <locale>en_US</locale> <!-- 指定生成时使用英文区域 -->
                    <validationMessages>ValidationMessages</validationMessages> <!-- 绑定国际化消息文件 -->
                </additionalProperties>
            </configuration>
        </execution>
    </executions>
</plugin>

Gradle 插件配置示例

plugins {
    id "org.openapi.generator" version "6.6.0"
}

openApiGenerate {
    generatorName = "spring"
    inputSpec = "$projectDir/src/main/resources/openapi.yaml".toString()
    configOptions = [
        interfaceOnly: "true",
        useSpringBoot3: "true"
    ]
    additionalProperties = [
        locale: "en_US",
        validationMessages: "ValidationMessages"
    ]
}

2. 创建英文验证消息文件

在src/main/resources目录下创建ValidationMessages.properties,定义所有需要的验证错误消息,覆盖默认的俄语提示:

# 基础验证规则消息
javax.validation.constraints.NotNull.message = {field} must not be null
javax.validation.constraints.NotEmpty.message = {field} must not be empty
javax.validation.constraints.Size.message = {field} must be between {min} and {max} characters
javax.validation.constraints.Pattern.message = {field} does not match the required pattern: {regexp}

# 自定义字段名映射(可选,替换DTO属性名为更友好的名称)
cart.items[0].feedId = Cart item feed ID

3. 配置Spring Boot国际化支持

在application.properties中指定默认语言和消息源,确保Spring验证框架使用英文消息:

# 国际化配置
spring.messages.basename=ValidationMessages
spring.messages.default-locale=en_US
spring.messages.locale-resolver=fixed # 固定使用英文;若需根据请求头切换可改为accept-header

4. 调整OpenAPI YAML的验证规则(可选)

如果生成的DTO仍存在硬编码俄语消息,可在OpenAPI YAML中显式绑定国际化消息key:

components:
  schemas:
    Cart:
      type: object
      properties:
        items:
          type: array
          items:
            $ref: '#/components/schemas/CartItem'
    CartItem:
      type: object
      properties:
        feedId:
          type: string
          nullable: false
          x-constraints:
            notNull:
              message: "{javax.validation.constraints.NotNull.message}" # 绑定国际化消息key

完成以上配置后,重新生成DTO并启动应用,验证错误提示将自动切换为英文格式,例如cart.items[0].feedId must not be null。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 22:05:20