Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Required MultipartFile parameter is not present - Spring Boot REST POST

Tags:

I'm trying to do a proof of concept that involves a .Net system posting a file to a Rest endpoint on a Java Spring Boot application. I keep getting the "Required Parameter is not present" error. There are a lot of SO questions with this error, and I've attempted the solutions presented in those with no luck. Can anyone see what I'm doing wrong?

Here's my C# code:

    private async Task<string> PostFileAsync(string uri, System.IO.FileStream fileStream)
    {
        using (var client = _httpClientFactory())
        {
            using (var content = new MultipartFormDataContent())
            {
                content.Add(new StreamContent(fileStream), "assetFile");
                using (var message = await client.PostAsync(uri, content))
                {
                    return await message.Content.ReadAsStringAsync();
                }
            }
        }
    }

Here's the request as Fiddler sees it:

POST http://10.0.2.2:8083/asset/1000/1001 HTTP/1.1
Content-Type: multipart/form-data; boundary="bac9aebd-d9ff-40ef-bcf3-4fffdd1b2c00"
Host: 10.0.2.2:8083
Content-Length: 158
Expect: 100-continue
Connection: Keep-Alive

--bac9aebd-d9ff-40ef-bcf3-4fffdd1b2c00
Content-Disposition: form-data; name=assetFile

foo,bar,10
foo2,bar2,12
--bac9aebd-d9ff-40ef-bcf3-4fffdd1b2c00--

Here's my Controller:

@RestController
@RequestMapping("/asset/")
public class AssetController {
    @RequestMapping(path="{merchantId}/{assetId}", method=RequestMethod.POST)
    public String getAsset(
            HttpServletRequest request,
            @RequestParam("assetFile") MultipartFile file,
            @PathVariable("merchantId") long merchantId, 
            @PathVariable("assetId") long assetId) throws IOException
    {
        return "It worked!";
    }
}

Here's my config:

@SpringBootApplication(exclude={MultipartAutoConfiguration.class})
public class MySpringApplication {

    public static void main(String[] args) {
        SpringApplication.run(MySpringApplication.class, args);
    }

    @Bean(name = "multipartResolver")
    public CommonsMultipartResolver multipartResolver() {
        System.out.println("multipartResolver()");
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        return multipartResolver;
    }
}

And here's the response:

HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 25 Mar 2016 19:34:55 GMT
Connection: close

f3
{"timestamp":1458934495566,"status":400,"error":"Bad Request","exception":"org.springframework.web.bind.MissingServletRequestParameterException","message":"Required MultipartFile parameter 'assetFile' is not present","path":"/asset/1000/1001"}
0

Edited because I posted the wrong C# code

like image 731
Homr Zodyssey Avatar asked Mar 25 '16 20:03

Homr Zodyssey


1 Answers

Ok, maybe I hadn't tried ALL of the solutions I saw on SO.

This question had a solution for me.

I had to use @ModelAttribute rather than @RequestParam.

like image 117
Homr Zodyssey Avatar answered Nov 03 '22 18:11

Homr Zodyssey