【发布时间】:2018-06-10 05:03:34
【问题描述】:
我有一个这样的控制器:
@RequestMapping(value = "/user/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public UserLesserDTO createUser(@RequestParam("profileImageFile") MultipartFile file, @RequestBody UserDTO user, @PathVariable("method") String method) {
// Save profileImage as a profile image and store the image file name in user's profileImage attribute.
// save user.
}
我有一个 HTML 表单,其中所有用户的属性为 <input type="text" name="user attribute name"> 加上一个 <input type="file" name="profileImageFile">
我收到错误 415,例如:The origin server is refusing to service the request because the payload is in a format not supported by this method on the target resource.
但是当我从 createUser() 方法的签名中删除 @RequestBody UserDTO user 时,上传文件部分开始工作,我可以获取文件。
@InitBinder
public void initBinder(WebDataBinder binder, WebRequest request) {
binder.setDisallowedFields("user.profileImage");
binder.setAllowedFields("profileImageFile");
}
但这并没有改变。
【问题讨论】:
-
你在使用 Spring Data 吗?
-
我不知道。正是我在这里展示的。需要查看我的 POM? github.com/icemagno/geoinfra/blob/master/cerberus/hades/pom.xml
-
恕我直言 Spring 混淆了如何处理属性。 Struts 使用 html 中的模型映射模型属性,例如 user.name
-
@PaulWarren 没有。我正在实现自己的存储库,但我的问题是关于表示层的,与持久性无关。即使我没有持久性,问题是:我无法将文件和模型数据从 Web 界面 (html) 发布到控制器。与持久层无关。
-
我的上帝。我在网上看到的每个教程都只是发布文件或模型数据。没有人,我说没有人同时发布。现在我知道为什么 Facebook 只允许你更新你的照片或个人资料数据,但不能同时更新。耶稣!
标签: spring-mvc