I am creating an application wherein there is a form & sub-form.
The form consists of Product Details. Sub-form consists of Color Details of the Product.
The frontend API request body is shown below.
{
"raw_material_id": 1137,
"extra_wip": "",
"is_active": "1",
"size_ratio_name_id": "45",
"is_new": 0,
"created_at": null,
"mrp": 125,
"collection": null,
"hsn_code": "62113300",
"is_sync": 0,
"extra_fabric_programming": "",
"sub_category_name": "INDOWESTERN SET",
"updated_at": null,
"category_name": "MENS",
"is_update": 0,
"name": "8642",
"season": null,
"comment": "",
"id": 1924,
"itm_no": "",
"seasonID": 7,
"collectionID": 3,
"raw_material_colors": [
{
"color_id": 123,
"color_name": "test",
"item_image": (binary) (This is nothing but Multipart File)
},
{
"color_id": 456,
"color_name": "test1",
"item_image": (binary) (This is nothing but Multipart File)
}
]
}
Content-Type Details:
Content-Disposition: form-data
I am attaching the UI screenshot to help you understand better.
I want to understand how to handle Images here in Spring boot.
I am also attaching the code that i tried.
@PutMapping( value="mainitem/{id}" , consumes = { MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE })
public ResponseEntity<?> user(HttpServletRequest request, @PathVariable Integer id,@RequestBody MainItem mainItem,
@RequestPart MultipartFile files) {
MainItem mainItem2 = mainItemrepo.findById(id).get();
mainItem2.setId(mainItem.getId());
mainItem2.setCollection(collectionRepo.collectionForMainItem(mainItem.getCollectionID()));
mainItem2.setSeason(seasonRepo.seasonForMainItem(mainItem.getSeasonID()));
mainItem2.setCategory_id(categoryrepo.categoryForMainItem(mainItem.getCategory_name()));
mainItem2.setSub_category_id(subcategoryrepo.subCategoryForMainItem(mainItem.getSub_category_name()));
mainItem2.setComment(mainItem.getComment());
mainItem2.setCreated_at(mainItem.getCreated_at());
mainItem2.setUpdated_at(mainItem.getUpdated_at());
mainItem2.setExtra_fabric_programming(mainItem.getExtra_fabric_programming());
mainItem2.setExtra_wip(mainItem.getExtra_wip());
mainItem2.setHsn_code(mainItem.getHsn_code());
mainItem2.setIs_active(mainItem.getIs_active());
mainItem2.setIs_new(mainItem.getIs_new());
mainItem2.setIs_sync(mainItem.getIs_sync());
mainItem2.setIs_update(mainItem.getIs_update());
mainItem2.setItm_no(mainItem.getItm_no());
mainItem2.setMrp(mainItem.getMrp());
mainItem2.setName(mainItem.getName());
mainItem2.setRaw_material_id(mainItem.getRaw_material_id());
mainItem2.setSize_ratio_name_id(mainItem.getSize_ratio_name_id());
mainItemrepo.save(mainItem2);
//Colors
for(Map<String, Object> listMap:mainItem.getRaw_material_colors()) {
System.out.println(listMap.get("color_id").toString());
System.out.println(listMap.get("color_name").toString());
System.out.println(listMap.get("item_image").toString());
}
return ResponseEntity.status(HttpStatus.OK)
.body(new error(200, "Data Edited Successfully", "OK", request.getRequestURI()));
}
Please help me here on how to handle such requests.
Thank you!