OpenApi 使用 JSON 发送多部分文件请求,不支持"应用程序/八位字节流"错误



我使用的是Spring Boot,我想使用Swagger UI发送带有json的MultipartFile,但如果我使用Postman,我会收到错误'application/octet-stream' error not supported

@RequestMapping(value = "/upload", method = RequestMethod.POST,
produces = { "application/json" },
consumes = { "multipart/form-data" })
public String hello(
@RequestPart(value = "file") MultipartFile file,
@RequestPart("grupo") Grupo grupo) {
if (file != null) {
logger.info("File name:  " + file.getOriginalFilename());
}
logger.info(grupo.toString());
return grupo.toString();
}

如何解决此问题?

  • springdoc openapi ui 1.4.4
  • 弹簧套2.3.2.RELEASE
  • 弹簧靴启动器腹板
  • Maven
  • 春季启动启动器数据jpa

要使用multipartFile发送json,请使用类型为"string"、格式为"binary"的注释@Parameter,这样您就可以发送json格式的文件。

@Parameter(schema =@Schema(type = "string", format = "binary"))

然后就会变成这样。

@PostMapping(value = "/test", consumes = MediaType.MULTIPART_FORM_DATA_VALUE )
public ResponseEntity<Void> saveDocu2ment(
@RequestPart(value = "personDTO") @Parameter(schema =@Schema(type = "string", format = "binary")) final PersonDTO personDTO,
@RequestPart(value = "file")  final MultipartFile file) {
return null;
}

参考-带有JSON的多部分请求-GitHub Springdoc openApi

最新更新