【问题标题】:How to send request to backend spring rest controller with a json requestbody and request param as a multipart image file如何使用 json requestbody 将请求发送到后端 spring rest 控制器并将请求参数作为多部分图像文件
【发布时间】:2021-05-05 13:30:45
【问题描述】:

我正在编写一个后端 api,它使用带有多部分图像文件的 json 请求。我无法找到这样做的方法。 任何人都遇到过类似我的问题并找到了解决方案...???

【问题讨论】:

    标签: java json spring spring-boot multipartfile


    【解决方案1】:

    您可以定义一个接受 DTO 和 MultipartFile 参数的 Spring Controller 端点

    @RestController
    @RequestMapping("/svc")
    public class MyController {
    
      @PostMapping
      public String create(
        @RequestPart(name = "dto") Dto dto, // my Java DTO
        @RequestPart(value = "file") MultipartFile file) throws Exception {
      
    }
    

    如果您需要使用多个文件,您应该可以使用MultipartFile[] files

    【讨论】:

      猜你喜欢
      • 2011-05-06
      • 1970-01-01
      • 2017-11-24
      • 2015-11-09
      • 1970-01-01
      • 2021-02-09
      • 2017-10-23
      • 2017-05-03
      • 2014-03-07
      相关资源
      最近更新 更多