我正在使用swagger-akka-http为我的akka-http服务构建swagger文档。
我的服务具有接受Characteristic
的List
的POST方法。
@ApiOperation(value = "Fetch offerings by characteristics", httpMethod = "POST")
@ApiImplicitParams(Array(
new ApiImplicitParam(name = "characteristics", required = true,
dataTypeClass = classOf[List[Characteristic]], paramType = "body")
))
@ApiResponses(Array(
new ApiResponse(code = 200, response = classOf[Offering], responseContainer = "List")
))
def fetchOfferings: Route = post {
entity(as[List[Characteristic]]) { characteristics =>
// some logic
}
}
ApiImplicitParams
中的dataTypeClass = classOf[List[Characteristic]]
未按预期工作。生成的Swagger YAML中有以下结果:
parameters:
- in: "body"
name: "body"
description: "Characteristics"
required: true
schema:
type: "array"
items:
type: "object"
如何在请求正文中记录对象集合?
您可以这样做,接受post请求中的对象列表。
@ApiModel(value = "Request object")
case class Request(
@(ApiModelProperty@field)(
value = "Name",
name = "name",
required = true,
dataType = "string",
example = "DE",
allowEmptyValue = false)
name: String,
@(ApiModelProperty@field)(
value = "Marks",
name = "marks",
required = true,
dataType = "List[integer]",
example = "[10]",
allowEmptyValue = false)
marks: List[Int])
在你的邮寄路线上,你可以做这样的事情。
@Path("/student")
@ApiOperation(
value = "Returns student information POST request",
nickname = "getStudentDetails",
httpMethod = "POST",
responseContainer = "List",
code = 200,
response = classOf[StudentDetails])
@ApiImplicitParams(Array(
new ApiImplicitParam(
name = "body",
required = true,
dataType = "Request",
paramType = "body")
))