postman 不支持的媒体类型-- Spring Boot

fkvaft9z  于 2022-11-23  发布在  Postman
关注(0)|答案(1)|浏览(214)

我在spring Boot 中创建了一个服务来接收和发送一个图像,但是当我试图用postman发送图像时,我收到了下一个错误

{
    "timestamp": "2022-11-08T17:01:58.015+00:00",
    "status": 415,
    "error": "Unsupported Media Type",
    "message": "",
    "path": "/reto/getImage"
}

Spring代码为

@ApiOperation(value = "/getImage")
@RequestMapping(value = "getImage", method = RequestMethod.POST, produces = MediaType.ALL_VALUE, consumes = MediaType.ALL_VALUE)
public ResponseEntity<Resource> getImage(@RequestBody ImageOpenLayer imageOpenLayer) throws IOException {
    this.servicio.getImage(imageOpenLayer);
    InputStream in = new BufferedInputStream(new FileInputStream(new File("MicrosoftTeams-image.png")));
    InputStreamResource file = new InputStreamResource(in);
    return ResponseEntity
            .ok()
            .contentType(MediaType.valueOf("image/png"))
            .body(file);
}


public class ImageOpenLayer {
    private MultipartFile file;

    public MultipartFile getFile() {
        return file;
    }

    public void setFile(MultipartFile file) {
        this.file = file;
    }
}

Postman

如果我将主体从MultipartFile更改为toher变量,则服务响应正常

public class ImageOpenLayer {
    private String file;

    public String  getFile() {
        return file;
    }

    public void setFile(String file) {
        this.file = file;
    }
}

在 Postman

busg9geu

busg9geu1#

如果要使用multipart/form-data,则需要将输入参数从@RequestBody ImageOpenLayer imageOpenLayer更改为@RequestParam(value="file",required = false) MultipartFile file

相关问题