I need to upload files using angular and spring boot.
Angular service:
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json;multi-part/form-data'})
};
addProduct(productRequest: ProductRequest, creator: string, tradeReference: string, selectedImage: File) {
const trimCreator = creator.trim();
const trimTradeReference = tradeReference.trim();
const formData = new FormData();
formData.append('images', selectedImage);
return this.http.post<any>(baseUrl + '/addProduct', {formData, productRequest, trimCreator, trimTradeReference} , httpOptions)
.pipe(
catchError(this.handleError)
);
}
and my backend Rest service is the following:
@ApiOperation(value = "add product", response = Iterable.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "Successfully added product") })
@PostMapping("/addProduct")
public void addProduct(@RequestPart(value = "images") MultipartFile[] images,
@RequestPart(value = "product") Product product,
@RequestPart(value = "creator") @ApiParam(value = "creator") String creator,
@RequestPart(value = "tradeReference") @ApiParam(value = "tradeReference") String tradeReference
) throws Exception {
Arrays.asList(images)
.stream()
.forEach(image -> uploadImages(image, product));
traderServices.addProduct(product, creator, tradeReference);
}
The error:
{"timestamp":"2020-04-23T13:35:03.114+0000","status":500,"error":"Internal Server Error","message":"Current request is not a multipart request","trace":"org.springframework.web.multipart.MultipartException: Current request is not a multipart request\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestPartMethodArgumentResolver.resolveArgument(RequestPartMethodArgumentResolver.java:158)\r\n\tat ,"path":"/api/trader/addProduct"}
I tried to update PostMapping like
@PostMapping(value = "/'addProduct", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_VALUE})
also I deleted consumes information and header informations in angular service, but it doesn't work.
With postman, it works fine.
So can't understand why it doesn't work!
Thanks
