首页 > 解决方案 > 如何在 Spring Boot 中添加对 Swagger 的 I18N 支持

问题描述

我正在使用springdoc-openapi-ui swagger。我不想硬编码swagger documentation. 我想从属性文件中读取这些值。

当我尝试出现编译错误时The value for annotation attribute Operation.summary must be a constant expression.

我知道它正在寻找常量表达式,但我不想在我的代码中硬编码这些值。

在此处输入图像描述

请在此处找到我的控制器代码

@RestController
@PropertySource("classpath:test.properties")
public class TestController {

    @Autowired
    private TestService testService;

    @Autowired
    private static Environment environment;

    final String SUMMARY = environment.getProperty("operationSummary");

    @Operation(summary = SUMMARY, description = "Returns a list", tags = { "Test" })
    @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Successful operation") })
    @GetMapping(path = "/description/{id}")
    public List<Test> getDescriptionById(@PathVariable String id) {

        return testService.getDescriptionById(id);
    }
}

有没有办法在端点注释中添加不同语言的消息属性?

标签: javaspring-bootinternationalizationswaggerspringdoc-openapi-ui

解决方案


您拥有带有 swagger 注释的 spring 属性支持:

  • Spring属性解析器对@Info的支持:title-description-version-termsOfService
  • spring 属性解析器对@Info.license 的支持:name - url
  • spring 属性解析器对@Info.contact 的支持:name - email - url
  • @Operation 对 spring 属性解析器的支持:描述 - 总结
  • Spring属性解析器对@Parameter的支持:描述-名称
  • Spring 属性解析器对@ApiResponse 的支持:描述
  • 也可以为@OAuthFlow 声明安全 URL:openIdConnectUrl - authorizationUrl - refreshUrl - tokenUrl
  • 通过将 springdoc.api-docs.resolve-schema-properties 设置为 true 来支持 @Schema: name - title - description 的 spring 属性解析器

推荐阅读