Springdoc发送Multipart文件为application/x-www-form-urlencoded,而不是



我使用的是最新版本的openapi-ui 1.6.7,我无法使文件上传端点工作。这是我对参数的配置:

@PostMapping(
consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
@Operation(
summary = "Create a new FileResource",
requestBody = @RequestBody(description = "File to upload")
)
public ResponseEntity<FileResourceIdPublicApiDto> create(
@Parameter(
description = "File to upload",
required = true
)
@RequestPart
MultipartFile file

使用"Try out"按钮,我得到一个415不支持的媒体类型错误。请求报头的内容类型为:application/x-www-form-urlencoded

我想这就是错误的来源。从OpenApi生成的json看起来像这样:

{
"operationId": "create_4",
"parameters": [
...
],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"required": [
"file"
],
"type": "object",
"properties": {
"file": {
"type": "string",
"format": "binary",
"description": "File to upload"
}
}
}
}
},
"description": "File to upload"
},
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/FileResourceId"
}
}
},
"description": "OK"
}
},
"summary": "Create a new FileResource",
"tags": [
"File Resource"
]
}

我缺少什么来发送具有表单数据内容类型的正确请求?

这是两件事的结合:定义" consume = multipart "并使用RequestParam代替RequestPart。

使用springfox Swagger 2.0时不需要。

没有为2.0编写好的迁移指南真是令人恼火->3.0 .

对于我来说,将RequestPart替换为RequestParam完成了这项工作!顺便说一下,我使用的是openapi-ui 1.6.4。

最新更新