示例的 Swagger Scala 注释

Swagger Scala annotation for exemples

我在 scala 项目中使用 swagger 注释(使用 swagger-akka-http

并且我正在尝试提供一些有效负载的明确示例

试试这个:

 @POST
  @Consumes(Array(MediaType.APPLICATION_JSON))
  @Produces(Array(MediaType.APPLICATION_JSON))
  @Operation(summary = "Creates a spark job with a given config", description = "Creates a spark job with a given config", tags = Array("Spark One time Job"),
    requestBody = new RequestBody(content = Array(new Content(
      schema = new Schema(implementation = classOf[pmConfig]),
      examples = Array(new ExampleObject(value = """{
                                                   |  "schema": [
                                                   |            {
                                                   |                "format": "None",
                                                   |                "ColumnName": "Service ID",
                                                   |                "DataType": "string"
                                                   |            },
                                                   |            {
                                                   |                "format": "None",
                                                   |                "ColumnName": "Operation",
                                                   |                "DataType": "string"
                                                   |            },
                                                   |            {
                                                   |                "format": "None",
                                                   |                "ColumnName": "Agent",
                                                   |                "DataType": "string"
                                                   |            },
                                                   |            {
                                                   |                "format": "None",
                                                   |                "ColumnName": "Service Type",
                                                   |                "DataType": "string"
                                                   |            },
                                                   |            {
                                                   |                "format": "None",
                                                   |                "ColumnName": "Product",
                                                   |                "DataType": "string"
                                                   |            },
                                                   |            {
                                                   |                "format": "None",
                                                   |                "ColumnName": "Customer ID",
                                                   |                "DataType": "string"
                                                   |            },
                                                   |            {
                                                   |                "format": "None",
                                                   |                "ColumnName": "Agent Position",
                                                   |                "DataType": "string"
                                                   |            },
                                                   |            {
                                                   |                "format": "d.M.yy H:m",
                                                   |                "ColumnName": "End Date",
                                                   |                "DataType": "timestamp"
                                                   |            },
                                                   |            {
                                                   |                "format": "d.M.yy H:m",
                                                   |                "ColumnName": "Start Date",
                                                   |                "DataType": "timestamp"
                                                   |            }
                                                   |        ],
                                                   |  "Dataset_Name": "CallCenter",
                                                   |  "Dataset_Description": "CallCenter",
                                                   |  "Dataset_Source": {
                                                   |    "Source": "/services/databases/ORG_*******/CallCenter"
                                                   |  },
                                                   |  "Description": "Call Center analysis",
                                                   |  "Filter": [
                                                   |    {
                                                   |      "Description": "None",
                                                   |      "Name": "None",
                                                   |      "Type": "None",
                                                   |      "Value": "None"
                                                   |    }
                                                   |  ],
                                                   |  "Groups": [
                                                   |    {
                                                   |      "Description": "None",
                                                   |      "Name": "None",
                                                   |      "Type": "None",
                                                   |      "Value": "None"
                                                   |    }
                                                   |  ],
                                                   |  "ID": "PAM_000001",
                                                   |  "Mapping": {
                                                   |    "Activity": "Operation",
                                                   |    "Case_ID": "Service ID",
                                                   |    "End_time": "End Date",
                                                   |    "Other_attributes": "true",
                                                   |    "Requester": "None",
                                                   |    "Resource": "Agent",
                                                   |    "Resource_group": "Agent Position",
                                                   |    "Scheduled_end": "None",
                                                   |    "Scheduled_start": "None",
                                                   |    "Start_time": "Start Date"
                                                   |  },
                                                   |  "Name": "CallCenter",
                                                   |  "Organization": "01dxjp1rpa35bzcv1kvem9ffyk",
                                                   |  "Schedule": {
                                                   |    "Schedule": "every5min",
                                                   |    "isSchedule": "false"
                                                   |  },
                                                   |  "reference": "121423525253"
                                                   |}""".stripMargin))
    ))),
    responses = Array(
      new ApiResponse(responseCode = "200", description = "Add response",
        content = Array(new Content(schema = new Schema(implementation = classOf[ActionPerformed])))),
      new ApiResponse(responseCode = "500", description = "Internal server error",
        content = Array(new Content(schema = new Schema(implementation = classOf[ActionPerformed]))))))

给出像这样的编译错误:

[error] /Users/*****/Documents/github/*****/src/main/scala/com/*****/labs/orchestrator/api/ProcessMiningRoutes.scala:152:58: annotation argument needs to be a constant; found: scala.Predef.augmentString("{\n                                                   |  \"schema\": [\n                                                   |            {\n                                                   |                \"format\": \"None\",\n                                                   |                \"ColumnName\": \"Service ID\",\n                                                   |                \"DataType\": \"string\"\n                                                   |            },\n                                                   |            {\n                                                   |                \"format\": \"None\",\n                                                   |                \"ColumnName\": \"Operation\",\n                                                   |                \"DataType\": \"string\"\n                                                   |            },\n                                                   |            {\n                                                   |                \"format\": \"None\",\n                                                   |                \"ColumnName\": \"Agent\",\n                                                   |                \"DataType\": \"string\"\n                                                   |            },\n                                                   |            {\n                                                   |                \"format\": \"None\",\n                                                   |                \"ColumnName\": \"Service Type\",\n                                                   |                \"DataType\": \"string\"\n                                                   |            },\n                                                   |            {\n                                                   |                \"format\": \"None\",\n                                                   |                \"ColumnName\": \"Product\",\n                                                   |                \"DataType\": \"string\"\n                                                   |            },\n                                                   |            {\n                                                   |                \"format\": \"None\",\n                                                   |                \"ColumnName\": \"Customer ID\",\n                                                   |                \"DataType\": \"string\"\n                                                   |            },\n                                                   |            {\n                                                   |                \"format\": \"None\",\n                                                   |                \"ColumnName\": \"Agent Position\",\n                                                   |                \"DataType\": \"string\"\n                                                   |            },\n                                                   |            {\n                                                   |                \"format\": \"d.M.yy H:m\",\n                                                   |                \"ColumnName\": \"End Date\",\n                                                   |                \"DataType\": \"timestamp\"\n                                                   |            },\n                                                   |            {\n                                                   |                \"format\": \"d.M.yy H:m\",\n                                                   |                \"ColumnName\": \"Start Date\",\n                                                   |                \"DataType\": \"timestamp\"\n                                                   |            }\n                                                   |        ],\n                                                   |  \"Dataset_Name\": \"CallCenter\",\n                                                   |  \"Dataset_Description\": \"CallCenter\",\n                                                   |  \"Dataset_Source\": {\n                                                   |    \"Source\": \"/services/databases/ORG_*****/CallCenter\"\n                                                   |  },\n                                                   |  \"Description\": \"Call Center analysis\",\n                                                   |  \"Filter\": [\n                                                   |    {\n                                                   |      \"Description\": \"None\",\n                                                   |      \"Name\": \"None\",\n                                                   |      \"Type\": \"None\",\n                                                   |      \"Value\": \"None\"\n                                                   |    }\n                                                   |  ],\n                                                   |  \"Groups\": [\n                                                   |    {\n                                                   |      \"Description\": \"None\",\n                                                   |      \"Name\": \"None\",\n                                                   |      \"Type\": \"None\",\n                                                   |      \"Value\": \"None\"\n                                                   |    }\n                                                   |  ],\n                                                   |  \"ID\": \"PAM_000001\",\n                                                   |  \"Mapping\": {\n                                                   |    \"Activity\": \"Operation\",\n                                                   |    \"Case_ID\": \"Service ID\",\n                                                   |    \"End_time\": \"End Date\",\n                                                   |    \"Other_attributes\": \"true\",\n                                                   |    \"Requester\": \"None\",\n                                                   |    \"Resource\": \"Agent\",\n                                                   |    \"Resource_group\": \"Agent Position\",\n                                                   |    \"Scheduled_end\": \"None\",\n                                                   |    \"Scheduled_start\": \"None\",\n                                                   |    \"Start_time\": \"Start Date\"\n                                                   |  },\n                                                   |  \"Name\": \"CallCenter\",\n                                                   |  \"Organization\": \"******\",\n                                                   |  \"Schedule\": {\n                                                   |    \"Schedule\": \"every5min\",\n                                                   |    \"isSchedule\": \"false\"\n                                                   |  },\n                                                   |  \"reference\": \"121423525253\"\n                                                   |}").stripMargin
[error]                                                    |}""".stripMargin))
[error]                                                          ^
[error] /Users/*****/Documents/github/*****/src/main/scala/com/*****/labs/orchestrator/api/ProcessMiningRoutes.scala:62:28: expected annotation of type io.swagger.v3.oas.annotations.media.ExampleObject, found <error>
[error]       examples = Array(new ExampleObject(value = """{
[error]                            ^
[error] /Users/*****/Documents/github/*****/src/main/scala/com/tibco/labs/orchestrator/api/ProcessMiningRoutes.scala:60:55: expected annotation of type io.swagger.v3.oas.annotations.media.Content, found <error>
[error]     requestBody = new RequestBody(content = Array(new Content(
[error]                                                       ^
[error] /Users/*****/Documents/github/*****/src/main/scala/com/*****/labs/orchestrator/api/ProcessMiningRoutes.scala:60:23: expected annotation of type io.swagger.v3.oas.annotations.parameters.RequestBody, found <error>
[error]     requestBody = new RequestBody(content = Array(new Content(
[error]                       ^
[error] four errors found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 3 s, completed 2 Mar 2021, 12:56:47

但是删除 .StripMargin 方法有效。问题是这个招摇的例子很难读懂

"\n          |{\n          |  \"schema\": [\n          |            {\n          |                \"format\": \"None\",\n          |                \"ColumnName\": \"Service ID\",\n          |                \"DataType\": \"string\"\n          |            },\n          |            {\n          |                \"format\": \"None\",\n          |                \"ColumnName\": \"Operation\",\n          |                \"DataType\": \"string\"\n          |            },\n          |            {\n          |                \"format\": \"None\",\n          |                \"ColumnName\": \"Agent\",\n          |                \"DataType\": \"string\"\n          |            },\n          |            {\n          |                \"format\": \"None\",\n          |                \"ColumnName\": \"Service Type\",\n          |                \"DataType\": \"string\"\n          |            },\n          |            {\n          |                \"format\": \"None\",\n          |                \"ColumnName\": \"Product\",\n          |                \"DataType\": \"string\"\n          |            },\n          |            {\n          |                \"format\": \"None\",\n          |                \"ColumnName\": \"Customer ID\",\n          |                \"DataType\": \"string\"\n          |            },\n          |            {\n          |                \"format\": \"None\",\n          |                \"ColumnName\": \"Agent Position\",\n          |                \"DataType\": \"string\"\n          |            },\n          |            {\n          |                \"format\": \"d.M.yy H:m\",\n          |                \"ColumnName\": \"End Date\",\n          |                \"DataType\": \"timestamp\"\n          |            },\n          |            {\n          |                \"format\": \"d.M.yy H:m\",\n          |                \"ColumnName\": \"Start Date\",\n          |                \"DataType\": \"timestamp\"\n          |            }\n          |        ],\n          |  \"Dataset_Name\": \"CallCenter\",\n          |  \"Dataset_Description\": \"CallCenter\",\n          |  \"Dataset_Source\": {\n          |    \"Source\": \"/services/databases/ORG_01dxjp1rpa35bzcv1kvem9ffyk/CallCenter\"\n          |  },\n          |  \"Description\": \"Call Center analysis\",\n          |  \"Filter\": [\n          |    {\n          |      \"Description\": \"None\",\n          |      \"Name\": \"None\",\n          |      \"Type\": \"None\",\n          |      \"Value\": \"None\"\n          |    }\n          |  ],\n          |  \"Groups\": [\n          |    {\n          |      \"Description\": \"None\",\n          |      \"Name\": \"None\",\n          |      \"Type\": \"None\",\n          |      \"Value\": \"None\"\n          |    }\n          |  ],\n          |  \"ID\": \"PAM_000001\",\n          |  \"Mapping\": {\n          |    \"Activity\": \"Operation\",\n          |    \"Case_ID\": \"Service ID\",\n          |    \"End_time\": \"End Date\",\n          |    \"Other_attributes\": \"true\",\n          |    \"Requester\": \"None\",\n          |    \"Resource\": \"Agent\",\n          |    \"Resource_group\": \"Agent Position\",\n          |    \"Scheduled_end\": \"None\",\n          |    \"Scheduled_start\": \"None\",\n          |    \"Start_time\": \"Start Date\"\n          |  },\n          |  \"Name\": \"CallCenter\",\n          |  \"Organization\": \"01dxjp1rpa35bzcv1kvem9ffyk\",\n          |  \"Schedule\": {\n          |    \"Schedule\": \"every5min\",\n          |    \"isSchedule\": \"false\"\n          |  },\n          |  \"reference\": \"121423525253\"\n          |}\n          |"

为了便于阅读,我想保持格式完整

有什么提示吗?

好吧听起来很简单:

  @POST
  @Consumes(Array(MediaType.APPLICATION_JSON))
  @Produces(Array(MediaType.APPLICATION_JSON))
  @Operation(summary = "Creates a spark job with a given config", description = "Creates a spark job with a given config", tags = Array("Spark One time Job"),
    requestBody = new RequestBody(content = Array(new Content(
      schema = new Schema(implementation = classOf[pmConfigLiveApps]),
      examples = Array(new ExampleObject(value = """{
                                                     "schema": [
                                                               {
                                                                   "format": "None",
                                                                   "ColumnName": "Service ID",
                                                                   "DataType": "string"
                                                               },
                                                               {
                                                                   "format": "None",
                                                                   "ColumnName": "Operation",
                                                                   "DataType": "string"
                                                               },
                                                               {
                                                                   "format": "None",
                                                                   "ColumnName": "Agent",
                                                                   "DataType": "string"
                                                               },
                                                               {
                                                                   "format": "None",
                                                                   "ColumnName": "Service Type",
                                                                   "DataType": "string"
                                                               },
                                                               {
                                                                   "format": "None",
                                                                   "ColumnName": "Product",
                                                                   "DataType": "string"
                                                               },
                                                               {
                                                                   "format": "None",
                                                                   "ColumnName": "Customer ID",
                                                                   "DataType": "string"
                                                               },
                                                               {
                                                                   "format": "None",
                                                                   "ColumnName": "Agent Position",
                                                                   "DataType": "string"
                                                               },
                                                               {
                                                                   "format": "d.M.yy H:m",
                                                                   "ColumnName": "End Date",
                                                                   "DataType": "timestamp"
                                                               },
                                                               {
                                                                   "format": "d.M.yy H:m",
                                                                   "ColumnName": "Start Date",
                                                                   "DataType": "timestamp"
                                                               }
                                                           ],
                                                     "Dataset_Name": "CallCenter",
                                                     "Dataset_Description": "CallCenter",
                                                     "Dataset_Source": {
                                                       "Source": "/services/databases/ORG_******/CallCenter"
                                                     },
                                                     "Description": "Call Center analysis",
                                                     "Filter": [
                                                       {
                                                         "Description": "None",
                                                         "Name": "None",
                                                         "Type": "None",
                                                         "Value": "None"
                                                       }
                                                     ],
                                                     "Groups": [
                                                       {
                                                         "Description": "None",
                                                         "Name": "None",
                                                         "Type": "None",
                                                         "Value": "None"
                                                       }
                                                     ],
                                                     "ID": "PAM_000001",
                                                     "Mapping": {
                                                       "Activity": "Operation",
                                                       "Case_ID": "Service ID",
                                                       "End_time": "End Date",
                                                       "Other_attributes": "true",
                                                       "Requester": "None",
                                                       "Resource": "Agent",
                                                       "Resource_group": "Agent Position",
                                                       "Scheduled_end": "None",
                                                       "Scheduled_start": "None",
                                                       "Start_time": "Start Date"
                                                     },
                                                     "Name": "CallCenter",
                                                     "Organization": "*******",
                                                     "Schedule": {
                                                       "Schedule": "every5min",
                                                       "isSchedule": "false"

                                                     },
                                                     "reference": "121423525253"
                                                   }"""))
    ))),
    responses = Array(
      new ApiResponse(responseCode = "200", description = "Add response",
        content = Array(new Content(schema = new Schema(implementation = classOf[ActionPerformed])))),
      new ApiResponse(responseCode = "500", description = "Internal server error",
        content = Array(new Content(schema = new Schema(implementation = classOf[ActionPerformed]))))))

成功了