0

是否可以将多部分表单的内容捆绑到相应控制器方法中的单个对象中?

即我如何转换方法

@RequestMapping(value = "/mult", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<SomeBean> upload(@RequestBody MultipartFile multipartFile, @RequestParam String param)

进入具有以下签名的方法:

@RequestMapping(value = "/mult", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<SomeBean> upload(@RequestBody MultipartWrapper wrapper)

其中MultipartWrapper包含 multipartFile 和附加参数:

public class MultipartWrapper {
    private String param;
    private MultipartFile multipartFile;

    // Getters & Setters
}
4

1 回答 1

1

像在其他控制器中一样使用 @ModelAttribute:

@RequestMapping(value = "/mult", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<SomeBean> upload(@ModelAttribute("cmd") MultipartWrapper wrapper) {
    ...
}

@ModelAttribute("cmd")
public MultipartWrapper getWrapper() {
  return new MultipartWrapper();
}

public class MultipartWrapper {
  private String param;
  private MultipartFile multipartFile;

  // Getters & Setters
}
于 2013-10-16T14:29:55.973 回答