1. Java 学习社区首页
  2. Java
  3. Spring Boot

Spring Boot – Multipart – Unsupported Media Type

我想在一个帖子请求中发送一个文件和一个json模型。

请求映射如下所示:

  @PostMapping("{id}/files")
    public MyOutput create(@PathVariable String id, @RequestPart("request") MyInput input, @RequestPart("file") MultipartFile file) {
    // ...
    }

收到的错误:

{
    "timestamp": "Feb 7, 2019, 3:18:50 PM",
    "status": 415,
    "error": "Unsupported Media Type",
    "message": "Content type 'application/octet-stream' not supported",
    "trace": "org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported...,
    "path": "/tests/12345/files"
}

正解的配置方式

@RequestPart("file") MultipartFile file

替换:

@RequestParam(value = "file",required = false) MultipartFile file

原创文章,如若转载,请注明出自:http://www.javaobj.com/2020/03/spring-boot-multipart-unsupported-media-type/

发表评论

电子邮件地址不会被公开。 必填项已用*标注