Spring boot Spring boot OpenAPI 3下载包含JSON内容和附件的多部分
当我尝试下载带有Swagger 3.0模式定义的文件时,我看到一个406错误。多部分文件下载的模式定义如下所示Spring boot Spring boot OpenAPI 3下载包含JSON内容和附件的多部分,spring-boot,swagger,multipartform-data,Spring Boot,Swagger,Multipartform Data,当我尝试下载带有Swagger 3.0模式定义的文件时,我看到一个406错误。多部分文件下载的模式定义如下所示 "get": { "operationId": "getAttachment", "summary": "Retrieve attachments to a existing Ticket", "tags": [ "cha
"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);
}
@ApiOperation(value=“检索现有票据的附件”,昵称=“getAttachment”,注释
=“”,response=InlineResponse200.class,标记={“changeRequest”,})
@RequestMapping(value=“/changeRequest/attachment/{id}”,
产生={“多部分/表单数据”,“应用程序/json”},
method=RequestMethod.GET)
公共响应属性getAttachment(@PathVariable(“id”)字符串id){
附件lAttachmentMetadata=新附件();
setDescription(“这是一个示例描述”);
lAttachmentMetadata.setSize(2000年);
FileSystemResource fileSysResource=新的FileSystemResource(新文件(“C:\\Projects\\Service Assurance\\Chnage Mgmt\\Attachments\\attachment.txt”);
InlineResponse200 responseObject=新的InlineResponse200();
setFile(fileSysResource);
responseObject.setMetadata(lAttachmentMetadata);
返回ResponseEntity.ok().header(HttpHeaders.CONTENT\u),
“附件;文件名=\”“+fileSysResource.getFilename()+”\).header(“内容类型”,“多部分/表单数据”).body(响应对象);
}
当我调用该服务时,我看到返回了一个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"}"
周四10月15日03:46:39 IST 2020:调试: