如何在 Spring Boot 中添加对 Swagger 的 I18N 支持

How to add I18N support for Swagger in Spring Boot

我正在与 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);
    }
}

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

您有 spring 属性支持和 swagger 注释:

  • spring 属性 解析器对 @Info 的支持:标题 - 描述 - 版本 - termsOfService
  • spring 属性解析器对@Info.license的支持:name - url
  • spring 属性解析器支持@Info.contact: name - email - url
  • spring 属性 解析器对@Operation 的支持:描述 - 摘要
  • spring 属性解析器对@Parameter: description - name
  • 的支持
  • spring 属性解析器对@ApiResponse的支持:描述
  • 也可以为@OAuthFlow 声明安全 URL:openIdConnectUrl - authorizationUrl - refreshUrl - tokenUrl
  • spring 属性 解析器对 @Schema 的支持:名称 - 标题 - 描述,通过将 springdoc.api-docs.resolve-schema-properties 设置为真

很高兴分享答案,所以这对其他人有用
最后,我在 swagger documentation.

中得到了使用外部属性的答案

我们可以通过创建属性或 yaml 文件来外部化文档并用作 propertysource
然后我们可以在 swagger 注释中使用 key 作为 ${propertyname} 如下所示。

@Tag(name = "Person Data")
@PropertySource("classpath:person-data-controller.properties")
public class PersonDataController {

    @Operation(summary = "${person.summary}", description = "${person.description}")
    public getPersonData(){
    
    }
}

我们还可以在 SwaggerConfiguration class 上添加 @PropertySource("classpath:person-data-controller.properties") 以避免在每个控制器上重复