SPRING REST: The request was rejected because no multipart boundary was found

后端 未结 4 1178
别跟我提以往
别跟我提以往 2020-12-03 13:26

I did a POC for spring 3 rest multipart file upload. Its working fine. But when i tried integrating with my application i am facing issues.

It throws following exce

相关标签:
4条回答
  • 2020-12-03 13:48

    Are you using any security filters? My problem was solved by removing the Security Filter Chain. From this:

    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).addFilters(this.springSecurityFilterChain).build();
    

    to this:

    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    

    I opened an issue where I explain the details: https://jira.spring.io/browse/SPR-12114

    0 讨论(0)
  • 2020-12-03 13:49

    Don't supply Content-Type header in the request. It will work.

    0 讨论(0)
  • 2020-12-03 14:00

    @sermolaev is right in his answer.

    I want to share my experience related to this problem. I've encountered this problem in Postman, but I could not understand the root cause for it for a long time. My request template seemed to be correct cause Postman included boundary in it...

    Eventually I've discovered that when you're specifying Content-Type=multipart/form header by yourself, it overrides the one added automatically by Postman. And this leads to the same error as yours. My solution was as simple as removing Content-Type header.

    0 讨论(0)
  • 2020-12-03 14:13

    The problem isn't in your code - it's in your request. You're missing boundary in your multipart request. As it said in specification:

    The Content-Type field for multipart entities requires one parameter, "boundary", which is used to specify the encapsulation boundary. The encapsulation boundary is defined as a line consisting entirely of two hyphen characters ("-", decimal code 45) followed by the boundary parameter value from the Content-Type header field.

    This and this posts should also be helpful.

    0 讨论(0)
提交回复
热议问题