【发布时间】:2014-06-25 08:21:58
【问题描述】:
我有一个这样的控制器,我想提交一个带有文件上传的表单以及一些表单数据,如标签,如下所示。另外,我想使用@RequestBody 来做到这一点,这样我就可以在包装器上使用@Valid 注释,因为将添加更多变量。
public @ResponseBody WebResponse<Boolean> updateEUSettings(
final Locale locale,
@Validated @ModelAttribute final EUPSettingsWrapper endUserPortalSettingsWrapper) {
}
我的包装是:
public class EUPSettingsWrapper {
private String label;
private MultipartFile logo;
// getter , setters..etc...
}
但我想将它从 ModelAttributes 转换为 @RequestBody。
我正在尝试的方法是将文件上传分隔为请求参数,如下所示:
public @ResponseBody WebResponse<Boolean> updateEUSettings(
final Locale locale,
@Validated @RequestBody final EUPSettingsWrapper endUserPortalSettingsWrapper,
@RequestParam(value = "file1", required = true) final MultipartFile logo) {
endUserPortalSettingsWrapper.setLogo(logo);
// ...
}
在我的模拟 MVC 中,我正在设置:
getMockMvc().perform(fileUpload(uri).file(logo)
.accept(MediaType.APPLICATION_JSON)
.content(JSONUtils.toJSON(wrapper))
.contentType(MediaType.MULTIPART_FORM_DATA))
.andExpect(status().isOk());
但我收到这样的错误:
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'multipart/form-data' not supported
有没有人知道如何将分段文件上传与@RequestBody 一起使用?上面有什么我做错了吗?
【问题讨论】:
标签: spring spring-mvc multipart multipartform-data