如何将以下Swagger注释从Java转换为Kotlin?
@ApiResponses(value = { @ApiResponse(code = 200, message = "Given admin user found"),
@ApiResponse(code = 404, message = "..."),
@ApiResponse(code = 500, message = "..."),
@ApiResponse(code = 400, message = "..."),
@ApiResponse(code = 412, message = "...") })
这不起作用:
@ApiResponses(value = listOf(
ApiResponse(code = 200, message = "..."),
ApiResponse(code = 404, message = "..."),
ApiResponse(code = 500, message = "..."),
ApiResponse(code = 400, message = "..."),
ApiResponse(code = 412, message = "...") ))
错误是:
类型推断失败。预期类型不匹配:推断类型为List但预期ApiResponse
当我只使用一个@ApiResponse
代替listOf()
时,它会起作用,但我必须定义更多@ApiResponse(s)
。
我使用Swagger 2.5.0
答案 0 :(得分:12)
如http://blog.kubernetes.io/2015/06/the-distributed-system-toolkit-patterns.html中所述:
如果Java中的[Annotation]的value参数具有数组类型,它将成为Kotlin中的vararg参数
所以,为了使你的例子有用,你需要这样说:
@ApiResponses(
ApiResponse(code = 200, message = "..."),
ApiResponse(code = 404, message = "..."),
ApiResponse(code = 500, message = "..."),
ApiResponse(code = 400, message = "..."),
ApiResponse(code = 412, message = "...")
)
答案 1 :(得分:1)
对于Swagger 3,这是可行的方法:
@ApiResponses(value = [
ApiResponse(responseCode = "200", description = "...", content = [
(Content(mediaType = "application/json", array = (
ArraySchema(schema = Schema(implementation = DataModel::class)))))]),
ApiResponse(responseCode = "400", description = "...", content = [Content()]),
ApiResponse(responseCode = "404", description = "...", content = [Content()])]
)
此代码段还包含@Content
,@ArraySchema
和@Schema
注释示例。