Spring boot OpenAPI 3 download multipart with JSON content and attachment

Viewed 531

I am seeing a 406 error when I try to download a file with Swagger 3.0 schema definition. The schema definition for a multipart file download which looks something like the below.

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

It generates the following class when built using the swagger plugin which seems to be apt:

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

  @JsonProperty("file")
  private Resource file;

Following is the implementation generated:

    @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);

    }

When I invoke the service I see a 406 error returned

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 SERVICE TEST

Any help or pointers in the right direction would be much appreciated.

0 Answers
Related