Spring MVC @RequestBody give me an empty String when use with @RequestParam MultipartFile

本小妞迷上赌 提交于 2019-12-10 15:51:24

问题


I have a problem with this web service code

@Controller
@RequestMapping("/u_tutorial")
public class UploadTutorial
{
    @RequestMapping(value = "tutorial1", method = RequestMethod.POST, headers = "Accept=application/json")
    @ResponseStatus(value = HttpStatus.OK)
    public void upload(@RequestBody String body, @RequestParam List<MultipartFile> file, Principal principal,
        HttpServletRequest request)
    {
        System.out.println("body: " + body); // always empty

        for (MultipartFile mf : file)
        {
            System.out.println("file: " + mf.getOriginalFilename());
        }
    }
}

The situation is when I want to upload files with some data in the request body, my body variable always gives me an empty String.

But when I try to use only "@RequestBody String body" or "@RequestParam List file", It works like a charm.

I had done some searching and found that it is because the InputStream problem. Some suggested solution have already discussed but not quite related to my problem (or may be yes but i don't understand).

Can anyone give me a solution ? Thx.

PS 1. here is my library version

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.core.version>3.1.4.RELEASE</spring.core.version>
    <spring.security.version>3.1.3.RELEASE</spring.security.version>
    <spring.data.mongodb.version>1.1.0.RELEASE</spring.data.mongodb.version>
    <cglib.version>2.2</cglib.version>
    <aspectj.version>1.6.10</aspectj.version>
    <mongodb.driver.version>2.10.1</mongodb.driver.version>
    <!-- Logging -->
    <slf4j.version>1.6.1</slf4j.version>
    <log4j.version>1.2.14</log4j.version>
    <javax.jstl-taglibs.version>1.1.2</javax.jstl-taglibs.version>
    <jackson.version>1.9.3</jackson.version>
    <!-- Testing -->
    <mockito.version>1.8.5</mockito.version>
    <junit.version>4.8.2</junit.version>
    <!-- Plugins -->
    <maven.copy.plugin.version>0.2.3</maven.copy.plugin.version>
    <maven.compiler.plugin.version>2.3.2</maven.compiler.plugin.version>
    <maven.apt.plugin.version>1.0</maven.apt.plugin.version>
    <!-- Utilities -->
    <apache.common.io.version>2.4</apache.common.io.version>
    <apache.common.fileupload.version>1.2.2</apache.common.fileupload.version>
    <jodatime.version>2.1</jodatime.version>
</properties>

PS 2. sorry for my english.


回答1:


You can make the "body" string a hidden field on the form and post it as another @RequestParam rather than having it as a separate @RequestBody parameter. So your form would look something like this:

<form id="myForm" method="post" enctype="multipart/form-data">
    <input type="hidden" name="body" value="bodyValue"/>
    <input type="file" name="file"/>
    ...

The controller would look like this:

public void upload(@RequestParam String body, @RequestParam List<MultipartFile> file, Principal principal, HttpServletRequest request)
....



回答2:


I had come to think about my question in different point of view and found that, may be, the client cannot upload files and send data in the @RequestBody at the same time.

So, I had started catching the request with WireShark. The result was as I expected. The data is missing. The only left in the request is files.

Reference: Posting a File and Associated Data to a RESTful WebService preferably as JSON

The solution may be to re-construct my web service server's method.

THX.



来源:https://stackoverflow.com/questions/15502054/spring-mvc-requestbody-give-me-an-empty-string-when-use-with-requestparam-mult

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!