Spring boot OpenAPI 3 загружает составную часть с содержимым в формате JSON и вложением

#spring-boot #swagger #multipartform-data

#spring-boot #swagger #составная часть-данные

Вопрос:

При попытке загрузить файл с определением схемы Swagger 3.0 я вижу ошибку 406. Определение схемы для загрузки составного файла, которое выглядит примерно так, как показано ниже.

 "get": {
    "operationId": "getAttachment",
    "summary": "Retrieve attachments to a existing Ticket",
    "tags": [
      "changeRequest"
    ],
    "parameters": [
      {
        "required": true,
        "name": "id",
        "in": "path",
        "description": "Identifier of the Change Request",
        "schema": {
          "type": "string"
        }
      }
    ],
    "responses": {
      "200": {
        "description": "Ok",
        "headers": {
        },
        "content": {
          "multipart/form-data": {
            "schema": {
                "type": "object",
                "properties": {
                    "metadata": {
                        "$ref": "#/components/schemas/Attachment"
                    },
                    "file": {
                        "type": "string", 
                        "format":"binary",
                        "description": "Actual File Attachment"
                    }
                }
            }
          }
        }
      }
    }
}

  

Он генерирует следующий класс при сборке с использованием плагина swagger, который кажется подходящим:

 public class InlineResponse200   {
  @JsonProperty("metadata")
  private Attachment metadata = null;

  @JsonProperty("file")
  private Resource file;
  

Ниже приведена сгенерированная реализация:

     @ApiOperation(value = "Retrieve attachments to a existing Ticket", nickname = "getAttachment", notes 
    = "", response = InlineResponse200.class, tags={ "changeRequest", })
    @RequestMapping(value = "/changeRequest/attachment/{id}",
        produces = { "multipart/form-data", "application/json" }, 
        method = RequestMethod.GET)
    public ResponseEntity<InlineResponse200> getAttachment(@PathVariable("id") String id) {
        Attachment lAttachmentMetadata = new Attachment();
        lAttachmentMetadata.setDescription("This is a sample description");
        lAttachmentMetadata.setSize(2000);
        
        FileSystemResource fileSysResource = new FileSystemResource(new File("C:\Projects\Service Assurance\Chnage Mgmt\Attachments\attachment.txt"));
        InlineResponse200 responseObject = new InlineResponse200();
        responseObject.setFile(fileSysResource);
        responseObject.setMetadata(lAttachmentMetadata);
        
        return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,
                "attachment; filename=""   fileSysResource.getFilename()   """).header("Content-Type", "multipart/form-data").body(responseObject);

    }
  

Когда я вызываю службу, я вижу возвращаемую ошибку 406

 Thu Oct 15 03:46:39 IST 2020:DEBUG:<< "{"timestamp":"2020-10-14T22:16:39.258Z","status":406,"error":"Not Acceptable","message":"Could not find acceptable representation","path":"/changeManagement/api/v1/changeRequest/attachment/1234"}"
  

ТЕСТ СЛУЖБЫ SOAPUI REST

Любая помощь или указания в правильном направлении будут высоко оценены.