POSTMAN始终提供415不支持的媒体类型错误。标题包含带有边界的multipart / form-data,如下面的CURL调用。还试图用RequestBody替换RequestPart但没有成功。
我们是否需要在使用FilePart时以任何不同的方式从第5季开始调用多部分文件上传apis?
RESTContoller:
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public void uploads(@RequestPart("filePart") Flux<Part> fileparts) {
....
}
卷曲:
curl -X POST \
http://localhost:8080/upload \
-H 'accept: application/json' \
-H 'cache-control: no-cache' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-H 'postman-token: 2e850843-13d0-32d3-8734-227242da3303' \
-F filePart=@abc.txt
输出:
"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'text/plain' not supported",
修改
将上传参数从@RequestPart("filePart") Flux<Part> fileparts
更改为@RequestParam("file") MultipartFile file
但是有效。
我们不能在RequestPart中使用相同的curl调用吗?
答案 0 :(得分:0)
问题归结于pom.xml,其中同时添加了spring-webmvc和spring-webflux作为依赖项。似乎无法同时启用spring-webmvc
和spring-webflux
。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>${spring.boot.version}</version>
</dependency>