我正在尝试创建一种在同一请求中接受 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){
fiddler
标题
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”)。因此,在“名称”字段中使用"file",而不是由 fiddler 生成的“fieldNameHere”。您的请求正文应如下所示。
---------------------------acebdf13572468
Content-Disposition: form-data; name="file"; filename="file1.pdf"
Content-Type: application/pdf
<@INCLUDE *C:\Users\User\Desktop\file1.pdf*@>
---------------------------acebdf13572468--
关于java - Spring Rest 中带有 JSON 的多部分不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39051510/