【问题标题】:Unsupported Media Type from multi part file api多部分文件 api 中不支持的媒体类型
【发布时间】:2018-06-10 10:00:37
【问题描述】:

它总是从 POSTMAN 错误地给出 415 不支持的媒体类型。标头包含带有边界的多部分/表单数据,如下面的 CURL 调用所示。还尝试将 RequestPart 替换为 RequestBody ,但没有成功。

在使用 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 filePart=@abc.txt

输出:

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

编辑

将上传参数从 @RequestPart("filePart") Flux&lt;Part&gt; fileparts 更改为 @RequestParam("file") MultipartFile file 但是有效。

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

【问题讨论】:

  • 是否需要使用Flux api?
  • @Niver,使用 Flux 我们可以在上传文件的同时处理文件。所以这不是一个要求,但我想它也应该与 Flux 一起工作?
  • 由于它是 Spring 5 中的新介绍,我一直在问自己,您使用它的方式是否存在任何问题。

标签: spring spring-mvc spring-boot project-reactor


【解决方案1】:

问题是由于 pom.xml 同时添加了 spring-webmvc 和 spring-webflux 作为依赖项。 spring-webmvcspring-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>

【讨论】:

    猜你喜欢
    • 2021-10-15
    • 2019-07-01
    • 2018-06-01
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 2017-08-19
    相关资源
    最近更新 更多