【发布时间】:2019-10-07 18:03:04
【问题描述】:
我在使用 Spring 的响应式框架处理文件上传时遇到了一些问题。我想我正在关注文档,但无法摆脱这个 415 / Unsupported Media Type 问题。
我的控制器如下所示(根据此处的示例:https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-multipart-forms)
package com.test.controllers;
import reactor.core.publisher.Flux;
import org.springframework.http.MediaType;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.http.codec.multipart.Part;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public Flux<String> uploadHandler(@RequestBody Flux<Part> parts) {
return parts
.filter(part -> part instanceof FilePart)
.ofType(FilePart.class)
.log()
.flatMap(p -> Flux.just(p.filename()));
}
}
虽然发布到此端点,但总是给我相同的输出:
curl -X POST -F "data=@basic.ppt" http://localhost:8080/upload
---
"Unsupported Media Type","message":"Content type 'multipart/form-data;boundary=------------------------537139718d79303c;charset=UTF-8' not supported"
我也尝试过使用@RequestPart("data"),但得到了类似的Unsupported Media Type 错误,尽管是文件的内容类型。
似乎 Spring 在将这些转换为 Part.. 时遇到问题?我被困住了 - 感谢任何帮助!
【问题讨论】:
标签: java spring spring-boot spring-webflux project-reactor