我正在尝试在 spring boot 2.0 应用程序中使用新的响应式 web-mvc 实现。我正在尝试定义一种使用多部分文件但未能成功使其工作的方法:( - 我总是收到 415 错误。
一方面,我有一个包含以下请求映射的控制器:
@RequestMapping(method = RequestMethod.POST, path = "/myPath/{param}/{param2}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ResponseBody
public Mono<Void> postFile(
@RequestBody MultipartFile data,
@PathVariable("param") String param,
@PathVariable("param2") String param2,
@RequestHeader(name = HEADER_DATE, required = false) @DateTimeFormat(pattern = DATE_FORMAT) Instant instant
){
return fileService.handleData(Mono.just(data), param, param2, instant);
}
另一方面,我不得不在基本依赖项之上添加一个服务器,因为 netty 似乎不处理多部分文件。我因此添加了使应用程序自动配置能够匹配和满足的spring-boot-starter-tomcat
依赖项。MultipartAutoConfiguration
使用 curl 调用发布内容时:
curl 'Meta-Date: 20170101104532' --form "file=@file.bin" http://localhost:8082/myPath/foo/bar
激活调试日志时(logging.level.org.springframework.web=DEBUG
)我得到了这个异常:
org.springframework.web.server.UnsupportedMediaTypeStatusException: Request failure [status: 415, reason: "Content type 'multipart/form-data;boundary=------------------------58fa43b8f1a26de4' not supported"]
RequestBodyArgumentResolver
具有以下受支持的媒体类型的会引发此错误:[*/*, text/xml, application/*+json;charset=UTF-8, application/xml, text/plain;charset=UTF-8, application/x-www-form-urlencoded, application/json;charset=UTF-8]
由 9 提供DecoderHttpMessageReader
。
在发布之前,我还看了看:
- Spring MultiPart MediaType Unsupported这似乎与此处无关,因为我的 autoconf 报告包含以下条目:
MultipartAutoConfiguration#multipartResolver matched
- 使用 angularjs $http 将 content-type 设置为 utf-8添加标头设置
Content-Transfer-Encoding: binary
并没有改变任何东西。
我的理解是 Spring web 5.0 使用了一个新的请求解码器系统,因为我在 spring 4 spring boot 应用程序上找不到这些类,并且还没有DecoderHttpMessageReader
处理多部分文件我错过了什么吗?还是我应该等待一个实施?