多部分文件 api 不支持的媒体类型

问题描述 投票:0回答:2

POSTMAN 总是给出 415 unsupported media type 错误。标头包含带有边界的 multipart/form-data,如下面的 CURL 调用所示。还尝试用 RequestBody 替换 RequestPart 但没有成功。

使用 FilePart 时,我们是否需要以任何不同的方式调用 Spring 5 中的多部分文件上传 api?

REST控制器:

 @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 [email protected]

输出:

"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'text/plain' not supported",

编辑

将上传参数从

@RequestPart("filePart") Flux<Part> fileparts
更改为
@RequestParam("file") MultipartFile file
但是有效。

我们不能对RequestPart 使用相同的curl 调用吗?

spring spring-mvc spring-boot project-reactor
2个回答
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>

0
投票

我的 pom.xml 中有

spring-boot-starter-web
spring-boot-starter-webflux
依赖项。 所以当我在 application.properties 中使用
spring.main.web-application-type=reactive
时。对我来说效果很好。

© www.soinside.com 2019 - 2024. All rights reserved.