Hikaru Shindo
Hikaru Shindo

Reputation: 2913

How to allow empty MultipartFile in Spring Boot?

I'm build a Spring Boot project. I have a Controller that let user upload a file in the request like this,

@PostMapping("/contact")
public ResultObj contactus(
    @RequestParam("file") MultipartFile file, 
    @RequestParam("topic") String topic,
    @RequestParam("name") String name,
    @RequestParam("phoneNumber") String phoneNumber,
    @RequestParam("question") String question
) {
   ....
   ....
  }

Everything is working well if users send the file through a request. But when users send a request without file it will throw error,

Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present]

I try to set the file field to 0, null, '' on the client app but I still get the same error.

How to allow file to be empty ? Please help. Thanks a lot.

Upvotes: 0

Views: 1623

Answers (1)

Alien
Alien

Reputation: 15878

Use required = false on request param.

@RequestParam(name="file",required=false) MultipartFile file,

Upvotes: 3

Related Questions