Spring File Upload - 'Required request part is not present'
Add Bean to Config file.
@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(-1);
return multipartResolver;
}
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public ResponseEntity<?> upload(@RequestParam(value = "name") String
name,@RequestParam(value = "file") MultipartFile file){
// TODO check file is not null and save
return new ResponseEntity<>(HttpStatus.valueOf(200));;
}
I finally solved the issue and sharing my solution in case someone else may face the same problem.
@RestController
@RequestMapping("/")
public class ConfigurationController {
@Bean
public MultipartConfigElement multipartConfigElement() {
return new MultipartConfigElement("");
}
@Bean
public MultipartResolver multipartResolver() {
org.springframework.web.multipart.commons.CommonsMultipartResolver multipartResolver = new org.springframework.web.multipart.commons.CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(1000000);
return multipartResolver;
}
@PostMapping(value = "/config", consumes = "multipart/form-data")
public ResponseEntity<?> saveEnvironmentConfig(@RequestParam("password") String password, @RequestParam("file") MultipartFile submissions)
throws AdminAuthenticationException, ConfigurationException {
return ResponseEntity.ok().body(null);
}
}