我遇到了一个问题。我想使用Swagger在API的响应中发送一个对象列表。type
=“array”对我不起作用。我看到了一个主题Set List of Objects in Swagger API response,但它是一个旧版本的lib。注解已经更改。ApiResponse以前有responseContainer参数,但现在没有了。我有akka-http
服务器。
val akkaVersion = "2.5.17"
val akkaHttpVersion = "10.1.5"
libraryDependencies ++= Seq(
"javax.ws.rs" % "javax.ws.rs-api" % "2.0.1",
"com.github.swagger-akka-http" %% "swagger-akka-http" % "2.0.0",
"com.github.swagger-akka-http" %% "swagger-scala-module" % "2.0.2",
"com.typesafe.akka" %% "akka-http" % akkaHttpVersion,
"com.typesafe.akka" %% "akka-http-spray-json" % akkaHttpVersion,
"com.typesafe.akka" %% "akka-actor" % akkaVersion,
"com.typesafe.akka" %% "akka-stream" % akkaVersion,
"com.typesafe.akka" %% "akka-slf4j" % akkaVersion,
"ch.megard" %% "akka-http-cors" % "0.3.0",
"org.slf4j" % "slf4j-simple" % "1.7.25"
)
我创建了getroute并使用swagger注解对其进行了描述。
@GET
@Path("offer-statuses/all")
@Produces(Array("application/json"))
@Operation(
tags = Array("offers"),
summary = "update periods",
responses = Array(
new ApiResponse(
responseCode = "200",
description = "OfferName response",
content = Array(
new Content(schema = new Schema(`type` = "array", implementation = classOf[EnumRow])))
),
new ApiResponse(responseCode = "400",
description = "Bad Request",
content = Array(new Content(schema = new Schema(implementation = classOf[BadRequest])))),
new ApiResponse(responseCode = "403",
description = "Forbidden",
content = Array(new Content(schema = new Schema(implementation = classOf[String]))))
)
)
def allOfferStatuses: Route = {
path("offers" / "offer-statuses" / "all") {
get {
applicationEnumsService.listAllOfferStatuses()
}
}
}
def listAllOfferStatuses(): List[EnumRow]
case class EnumRow(id: Int, name: String)
并构建json:
"/api/v1/offers/offer-statuses/all" : {
"get" : {
"tags" : [ "offers" ],
"summary" : "update periods",
"operationId" : "allOfferStatuses",
"responses" : {
"200" : {
"description" : "OfferName response",
"content" : {
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/EnumRow"
}
}
}
},
"400" : {
"description" : "Bad Request",
"content" : {
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/BadRequest"
}
}
}
},
"403" : {
"description" : "Forbidden",
"content" : {
"application/json" : {
"schema" : {
"type" : "string"
}
}
}
}
}
}
}
},
"EnumRow" : {
"required" : [ "id", "name" ],
"type" : "object",
"properties" : {
"id" : {
"type" : "integer",
"format" : "int32"
},
"name" : {
"type" : "string"
}
}
},
2条答案
按热度按时间ddrv8njm1#
我找到了解决办法:
rryofs0p2#
这似乎适用于我从swagger上传的单个文件:
具有相依性: