本文介绍了swagger-akka-http 2.x 在响应中设置对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题.我想在使用 Swagger 的 API 响应中发送对象列表.type = "array" 对我不起作用.我看到一个主题 在 Swagger API 响应中设置对象列表,但它是旧版本的lib.注释已更改.ApiResponse 曾经有 responseContainer 参数,但现在没有了.我有 akka-http 服务器.

I have a problem. I want to send a list of objects in the response of an API using Swagger. type = "array" does not work for me.I saw a topic Set List of Objects in Swagger API response , but it is an old version of lib. Annotation has changed. ApiResponse used to have responseContainer param, but now it is gone.I have akka-http server.

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"
)

我创建了 get 路由并使用 swagger 注释对其进行了描述.

I create get route and describe it with swagger annotations.

  @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"
      }
    }
  },

推荐答案

这似乎对我从 swagger 上传单个文件有用:

This seems to work for me for a single file upload from swagger:

import java.io.File

import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.server.directives.FileInfo
import com.demo.erp.routes.SwaggerUi.{complete, path, post, storeUploadedFile}
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.media.{Content, Schema}
import io.swagger.v3.oas.annotations.parameters.RequestBody
import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.annotations.tags.Tag
import javax.ws.rs.core.MediaType
import javax.ws.rs.{Consumes, POST, Path}

@Tag(name = "File Upload")
class FileUploadRouter {

  //just for swagger
  case class FileUpload(@Schema(`type` = "string", format = "binary", description = "file") file: File)

  def tempDestination(fileInfo: FileInfo): File = File.createTempFile(fileInfo.fileName, ".tmp")

  val routes: Route = fileUpload

  @POST
  @Consumes(Array(MediaType.MULTIPART_FORM_DATA))
  @Path("upload")
  @Operation(
    summary = "File upload",
    description = "Upload file",
    requestBody = new RequestBody(
      description = "File",
      content = Array(
        new Content(
          mediaType = MediaType.MULTIPART_FORM_DATA,
          schema = new Schema(implementation = classOf[FileUpload])
        )
      )
    ),
    responses = Array(
      new ApiResponse(
        responseCode = "200",
        description = "File uploaded",
      ),
      new ApiResponse(responseCode = "400", description = "Bad request"),
      new ApiResponse(responseCode = "500", description = "Internal server error")
    )
  )
  def fileUpload: Route = post {
    path("upload") {
      storeUploadedFile("file", tempDestination) {
        case (metadata, file) =>
          // do something with the file and file metadata ...
          println("Metadata: " + metadata)
          println("File: " + file)
          complete(StatusCodes.OK, "Uploaded")
      }
    }
  }
}

有依赖项:

lazy val akkaHttpVersion = "10.2.1"
lazy val akkaVersion = "2.6.10"
lazy val swaggerVersion = "2.1.5"
lazy val jacksonVersion = "2.11.3"

lazy val root = (project in file("."))
  .enablePlugins(JavaAppPackaging)
  .settings(
    inThisBuild(List(organization := "com.demo.erp", scalaVersion := "2.13.4")),
    name := "demo-erp",
    libraryDependencies ++= Seq(
      "com.typesafe.akka"            %% "akka-http"                 % akkaHttpVersion,
      "com.typesafe.akka"            %% "akka-http-spray-json"      % akkaHttpVersion,
      "com.typesafe.akka"            %% "akka-actor-typed"          % akkaVersion,
      "com.typesafe.akka"            %% "akka-stream"               % akkaVersion,
      "com.typesafe.play"            %% "play-json"                 % "2.9.1",
      "org.mongodb.scala"            %% "mongo-scala-driver"        % "4.1.1",
      "ch.qos.logback"               % "logback-classic"            % "1.2.3",
      "ch.rasc"                      % "bsoncodec"                  % "1.0.1",
      "com.github.pjfanning"         %% "scala-faker"               % "0.5.0",
      "javax.ws.rs"                  % "javax.ws.rs-api"            % "2.1.1",
      "com.github.swagger-akka-http" %% "swagger-akka-http"         % "2.2.0",
      "com.github.swagger-akka-http" %% "swagger-scala-module"      % "2.1.3",
      "com.github.swagger-akka-http" %% "swagger-enumeratum-module" % "2.0.0",
      "com.fasterxml.jackson.module" %% "jackson-module-scala"      % jacksonVersion,
      "pl.iterators"                 %% "kebs-spray-json"           % "1.8.1",
      "io.swagger.core.v3"           % "swagger-core"               % swaggerVersion,
      "io.swagger.core.v3"           % "swagger-annotations"        % swaggerVersion,
      "io.swagger.core.v3"           % "swagger-models"             % swaggerVersion,
      "io.swagger.core.v3"           % "swagger-jaxrs2"             % swaggerVersion,
      "ch.megard"                    %% "akka-http-cors"            % "1.1.0",
      "com.typesafe.akka"            %% "akka-http-testkit"         % akkaHttpVersion % Test,
      "com.typesafe.akka"            %% "akka-actor-testkit-typed"  % akkaVersion % Test,
      "org.scalatest"                %% "scalatest"                 % "3.2.3" % Test
    )
  )

这篇关于swagger-akka-http 2.x 在响应中设置对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 22:36