Spring Boot OpenAPI 3下载包含JSON内容和附件的多部分

问题描述

当我尝试下载具有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 SERVICE TEST

任何帮助或正确方向的指点都将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)