Spring Controller @RequestBody可以上传文件吗?

Yat*_*tin 32 spring multipartform-data spring-mvc multipart

我有一个像这样的控制器,我想提交一个包含文件上传的表格以及一些表格数据,如下图所示.另外,我想使用@RequestBody这样做,所以我可以在包装器上使用@Valid注释,因为会添加更多变量.

public @ResponseBody WebResponse<Boolean> updateEUSettings(
    final Locale locale,
    @Validated @ModelAttribute final EUPSettingsWrapper endUserPortalSettingsWrapper) {
}
Run Code Online (Sandbox Code Playgroud)

我的包装是:

public class EUPSettingsWrapper {

    private String label;
    private MultipartFile logo;
// getter , setters..etc...
}
Run Code Online (Sandbox Code Playgroud)

但我想将它从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);

    // ...
}
Run Code Online (Sandbox Code Playgroud)

在我的模拟MVC中,我正在设置:

getMockMvc().perform(fileUpload(uri).file(logo)
                        .accept(MediaType.APPLICATION_JSON)
                        .content(JSONUtils.toJSON(wrapper))
                        .contentType(MediaType.MULTIPART_FORM_DATA))
                        .andExpect(status().isOk());
Run Code Online (Sandbox Code Playgroud)

但我收到的错误是这样的:

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'multipart/form-data' not supported
Run Code Online (Sandbox Code Playgroud)

有没有人知道如何将Multipart文件上传与@RequestBody一起使用?我上面做错了什么?

geo*_*and 38

你可以在这里简化你的生活,因为你所做的就是提交一个包含一些字段和文件的表单.你并不需要为你正在尝试做@RequestBody.您可以使用常规的Spring MVC功能,因此您的控制器方法如下所示:

@ResponseBody
public WebResponse<Boolean> updateEUSettings(
     Locale locale, 
     @Valid EUPSettingsWrapper endUserPortalSettingsWrapper, 
     @RequestParam(value = "file1", required = true) MultipartFile logo
) {


}
Run Code Online (Sandbox Code Playgroud)

向此控制器提交请求的客户端需要有一个表单enctype="multipart/form-data".

在你的Spring MVC测试中,你会写这样的东西:

getMockMvc().perform(fileUpload(uri).file("file1", "some-content".getBytes()) 
                        .param("someEuSettingsProperty", "someValue")
                        .param("someOtherEuSettingsProperty", "someOtherValue")
                        .accept(MediaType.APPLICATION_JSON)
                        .contentType(MediaType.MULTIPART_FORM_DATA))
                        .andExpect(status().isOk());
Run Code Online (Sandbox Code Playgroud)


Ank*_*ain 12

请在spring-servlet.xml中添加以下bean,以添加对multipart请求的支持.

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
Run Code Online (Sandbox Code Playgroud)

另外不要忘记为commons-fileupload jar添加依赖项


小智 5

我找不到使用@RequestBody的方法。

但是,您可以执行以下操作:

@RequestMapping(value = "/uploadStuff", method = RequestMethod.POST)
public MyViewDto doStuff(@RequestPart("json") @Valid MyDto dto,
                         @RequestPart("file") MultipartFile file) { ... }
Run Code Online (Sandbox Code Playgroud)

您可以像这样测试它:

MockMultipartFile jsonFile = new MockMultipartFile("json", "",
            "application/json", "{}".getBytes());
MockMultipartFile dataFile = new MockMultipartFile("file", "foo.zip", "application/octet-stream", bytes);

mockMvc.perform(fileUpload("/uploadStuff")
            .file(dataFile)
            .file(jsonFile))
            .andExpect(status().isOk());
Run Code Online (Sandbox Code Playgroud)

  • org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型“application/octet-stream” (2认同)