具有@RequestBody的Spring分段文件 [英] Spring Multipart File with @RequestBody
问题描述
我正在尝试将数据从应用程序上传到Spring后端服务. 要上传的内容是一个DataModel,其中包含要创建的对象的数据以及链接到该数据的几个图像. 因此,我正在使用此方法签名:
I am trying to upload data from an app to a spring backend service. Things to upload are a DataModel containing data of the object to create and several images linked to the data. Therefore I am using this method signature:
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public Survey createSurvey(@RequestBody SurveyPostHelper helper, @RequestParam(value="file", required = true) MultipartFile[] images)
我尝试使用批注,但是要么得到一个空白图像数组,要么我的助手为空. 您将如何解决这个问题?
I tried to play with the annotations, but either I get a blank images array or my helper is empty. How would you solve this?
提前谢谢.
推荐答案
我发现,此方法签名可以完成这项工作:
I found out, that this method signature could do the job:
@ResponseBody
public Survey createSurvey(@RequestPart(required=true) SurveyPostHelper helper, @RequestPart(value="file", required = true) final MultipartFile[] images)
在我的情况下,重要的是在客户端应用程序中设置MimeType.文件MimeType应该为image/jpg
,而SurveyPostHelper
s为application/json
,以允许Spring解析json并将其绑定到我的Object.
Important in my case was to set the MimeType in the client app. The files MimeType should be image/jpg
and the SurveyPostHelper
s to application/json
to allow Spring to parse the json and bind it to my Object.
这篇关于具有@RequestBody的Spring分段文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!