本文介绍了Spring Controller @RequestBody可以上传文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的控制器,我想提交一个带有文件上传功能的表单,以及一些表单数据(如标签),如下所示.另外,我想使用@RequestBody进行此操作,因此可以在包装器上使用@Valid注释,因为将添加更多变量.

I have a Controller like this and I want to submit a form with file uploading as well as some form data like label as shown below. Also, I want to do that using @RequestBody so I can use the @Valid annotation on the wrapper as more variables will be added.

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.

But I would like to convert it into a @RequestBody from ModelAttributes.

我正在尝试的方法是将文件上传作为请求参数分开,如下所示:

The way I'm trying is by having the file upload separated as request parameter like this:

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中,我正在设置:

In my mock MVC, I am setting:

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

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

But I'm getting an error like this which says:

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'multipart/form-data' not supported

有人对@RequestBody如何使用分段文件上传有个好主意吗?上面我做错了什么?

Does anyone have a good idea of how Multipart file uploads can be used with @RequestBody? Anything I am doing wrong above?

推荐答案

您实际上可以在这里简化生活,因为您所做的只是提交包含某些字段和文件的表单.您不需要即可使用@RequestBody进行操作.您可以使用常规的Spring MVC功能,因此您的控制器方法如下所示:

You can actually simplify your life here since all you are doing is submitting a form that contains some fields and file.You don't need @RequestBody for what you are trying to do. You can use regular Spring MVC features, so your controller method would look like:

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


}

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

The client that submits the request to this controller will need to have a form with enctype="multipart/form-data".

在Spring MVC测试中,您将编写如下内容:

In your Spring MVC test you would write something like this:

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());

这篇关于Spring Controller @RequestBody可以上传文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-09 22:21