簡體   English   中英

如何在 Spring 引導中添加對 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.

我知道它正在尋找常量表達式,但我不想在我的代碼中硬編碼這些值。

在此處輸入圖像描述

請在此處找到我的 controller 代碼

@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);
    }
}

有沒有辦法在端點注釋中添加不同語言的消息屬性?

很高興分享答案,所以這對其他人有用
最后,我在swagger documentation中得到了使用外部屬性的答案。

我們可以通過創建屬性或 yaml 文件來外部化文檔並用作propertysource
然后我們可以在 swagger 注釋中使用密鑰作為${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")以避免在每個 controller 上重復

你有 spring 屬性支持 swagger 注釋:

  • spring 屬性解析器對@Info 的支持:title - description - version - termsOfService
  • 支持@Info.license 的 spring 屬性解析器:名稱 - url
  • 支持@Info.contact 的 spring 屬性解析器:名稱 - email - url
  • spring 屬性解析器對@Operation 的支持:描述 - 總結
  • spring 屬性解析器對@Parameter: description - name 的支持
  • 對@ApiResponse 的 spring 屬性解析器的支持:說明
  • 也可以為@OAuthFlow 聲明安全 URL:openIdConnectUrl - authorizationUrl - refreshUrl - tokenUrl
  • 通過將 springdoc.api-docs.resolve-schema-properties 設置為 true,支持 @Schema 的 spring 屬性解析器:名稱 - 標題 - 描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM