2 回答

TA貢獻1820條經驗 獲得超10個贊
您可以定義單獨的注釋:
annotation class MyOwnApi(
val openApi: OpenApi = OpenApi(
summary = "",
description = "Lists all customers",
path = "customers",
queryParams =
// ...........
// ...........
// etc
)
)
annotation class UserOpenApi(
val openApi: OpenApi = OpenApi(
summary = "Something",
description = "Lists all users",
// ...........
// ...........
// etc
)
)
優點:
代碼分離
可重用的注釋類
您甚至可以創建一個構建器類并測試它
缺點:
令人困惑
注解不能繼承、擴展類或實現接口
如果直接檢查類/對象,可能無法
@OpenApi
實現或需要復雜的代碼更改。在這種情況下,您將需要另一個反射搜索來從字段中提取注釋!

TA貢獻1775條經驗 獲得超11個贊
好的,您想要的是@OpenApi將文檔與 REST 處理程序代碼分開。您可以通過刪除實現而不是刪除注釋來做到這一點。
因此,在所有注釋與 REST 處理程序代碼混合的當前文件中@OpenApi,您可以調用輔助函數,如下所示:
@OpenApi(
summary = "",
description = "Lists all customers",
path = "customers",
queryParams =
// ...........
// ...........
// etc
)
override fun handle(context: Context) {
handleGetCustomers(context)
}
然后您將所有 REST 處理程序放入該文件的頂部或另一個文件中以進行更多隔離,這樣您就可以在它們之間滾動,而不會受到注釋的干擾@OpenApi:
// Collected at the top of the file, or in a separate file
fun handleGetCustomers(context: Context) {
// body of the REST handler
}
然后,您可以輕松地在 REST 處理程序代碼之間滾動,而不會受到@OpenApi噪音的困擾。
請注意,您應該使用Android Studio 的“轉到” -> “定義”功能,以避免滾動到handleGetCustomers().
添加回答
舉報