I have an angular form where I fill fields for a product and then I select images. I've added following configuration in AppConfig in SpringMVC project to enable MultiPart Files.
@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(1000000);
return multipartResolver;
}
I've also added a transient field in my Product Model for images like this:
@Transient
private List<MultipartFile> productImages;
Since I want to receive the Product object separately inside the controller I'm using @RequestPart
to fetch both separately like this:
@RequestMapping(value = "save", method = RequestMethod.POST)
public ResponseEntity addProduct(@Valid @RequestPart Product product, @RequestPart MultipartFile[] images, BindingResult bindingResult, HttpServletRequest
}
And on the frontend inside Angular, I'm trying to use FormData
to help with this.
let formData = new FormData();
formData.append('product', new Blob([JSON.stringify(this.product)],{ type: "application/json" }));
// I iterate and append all the images like this
formData.append('image[]', this.images, this.images.name);
this.http.post(this.appService.getApiUrl() + "api/product/save/", product);
The problem is that whenever I submit the form, I get this exception as a response: HTTP Status 415 – Unsupported Media Type
I had an HttpInterceptor that appended an application/json header for Content-Type, I removed it because of it I was getting HTTP Status 400 – Bad Request. At this point, I do not know what's the problem, I don't know how to debug the front facing spring mvc stuff, I can only debug through breakpoints within a controller method but the problem arises well before.