如何解决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"}"
任何帮助或正确方向的指点都将不胜感激。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。