Good Day devs
I'm trying to upload a property object (JSON) along with multiple images using multipart/form-data But Spring Boot keeps returning:
415 Unsupported Media Type
Content-Type 'application/octet-stream' is not supported.
my controller
@PostMapping(consumes = {"multipart/form-data"})
public ResponseEntity createProperty(
@RequestPart(value = "property") @Valid PropertyRequestDto requestDto,
@RequestPart(value = "images", required = false) MultipartFile[] images) throws IOException {
if (images != null && images.length > 0) {
List uploadedUrls = cloudinaryService.uploadImages(images);
requestDto.setImages(uploadedUrls);
}
PropertyResponseDto responseDto = propertyService.createProperty(requestDto);
URI location = ServletUriComponentsBuilder
.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(responseDto.getId())
.toUri();
return ResponseEntity.created(location).body(responseDto);
}
Cloudinary Service:
public class CloudinaryService {
private final Cloudinary cloudinary;
public String uploadImage(MultipartFile file) throws IOException {
Map uploadResult = cloudinary.uploader().upload(file.getBytes(), ObjectUtils.asMap(
"use_filename", true,
"unique_filename", false,
"overwrite", true
));
return uploadResult.get("secure_url").toString();
}
public List uploadImages(MultipartFile[] files) throws IOException {
List urls = new ArrayList<>();
for (MultipartFile file : files) {
urls.add(uploadImage(file));
}
return urls;
}
}
Enitity(Property Model)
@ElementCollection
@CollectionTable(name = "property_images", joinColumns = @JoinColumn(name = "property_id"))
@Column(name = "image_url")
private List images = new ArrayList<>();
Postman Request Setup
KeyTypeValuepropertytext(JSON body)imagesfile*.jpg images
But whenever I include images, Spring throws the 415 Unsupported Media Type.
If I remove the images field, everything works.
What I Have Tried
Adding
consumes = {"multipart/form-data"}on the controllerMaking sure Postman uses
multipart/form-data
I need help pleasee