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