在 spring 中使用 JSON 的多部分 rest 不工作

Multipart with JSON in spring rest is not working

我正在尝试创建一个在同一请求中接受 multipart/form-data 和 application/json 内容类型的方法。我正在使用 Spring Rest 来创建它。但是当我从 Fiddler 请求时,调用没有到达方法。请帮忙

服务方式

@RequestMapping(value = "/", method = RequestMethod.POST, produces = "application/json",consumes = "multipart/form-data")
public ResponseEntity<JSONObject> addField( @RequestParam int customerId,@RequestParam int teId, 
        @RequestPart("file") List<MultipartFile> multipartFiles, @RequestParam("toast") String toast, MultipartHttpServletRequest request){

提琴手

页眉

Content-Type: multipart/form-data; boundary=HereGoes;

正文

 Content-Type: application/json

 ---------------------------acebdf13572468
  Content-Disposition: form-data; name="fieldNameHere"; filename="file1.pdf"
Content-Type: application/pdf

  <@INCLUDE *C:\Users\User\Desktop\file1.pdf*@>
 ---------------------------acebdf13572468--

在fiddler的请求正文中添加正确的文件名。在您的方法中它是@RequestPart("file")。因此,在 "name" 字段中使用 "file" 而不是由提琴手生成的 "fieldNameHere" 。您的请求正文应如下所示。

 ---------------------------acebdf13572468
 Content-Disposition: form-data; name="file"; filename="file1.pdf"
 Content-Type: application/pdf

 <@INCLUDE *C:\Users\User\Desktop\file1.pdf*@>
 ---------------------------acebdf13572468--