Проблема с контроллером, который принимает base64 картинку в Spring
Итак, я разрабатываю интернет-магазин и в нем есть возможность добавлять картинку. На React я кодирую картинку в base64 и передаю на сервер спринга в качестве строки.
export default class DishesService {
static async addDish(dish: IDish, file: any) {
try {
await axios.post<IDish>('http://localhost:8080/dishes', dish)
.then(response => {
this.updateDishImage(response.data.id, file)
})
} catch (e) {
console.log('произошла ошибка при добавлении блюда')
}
}
static async updateDishImage(id: number | undefined, image: any) {
try {
await axios.put('http://localhost:8080/dishes/' + id, {base64File: image})
}
catch (e) {
console.log('Произошла ошибка при добавлении картинки к блюду')
}
}
}
Где file - это закодированная в base64 строка.
Теперь мой контроллер:
@PostMapping
public ResponseEntity<DishEntity> saveDish(@RequestBody DishEntity dish) {
DishEntity response = dishService.save(dish);
return ResponseEntity.ok(response);
}
@PutMapping("{dishId}")
public ResponseEntity<DishEntity> updateDishImage(@PathVariable Long dishId, @RequestBody String base64File) {
DishEntity response = dishService.updateDishImage(base64File, dishId);
return ResponseEntity.ok(response);
}
@Override
public DishEntity updateDishImage(String base64File, Long dishId) {
DishEntity dishById = findById(dishId);
log.info("Updating dish image: {}", dishId);
try{
byte[] byteImage = Base64.decodeBase64(base64File);
dishById.setImage(byteImage);
log.info("byteImage: {}", byteImage);
log.info("dish.getImage(): {}", dishById.getImage());
}catch (Exception e){
throw new OperationFailedException("Base64 decoding failed!");
}
DishEntity updatedDish;
try {
updatedDish = dishRepository.save(dishById);
} catch (Exception ex) {
throw new OperationFailedException("Update dish image method failed!");
}
return updatedDish;
}
Но до updateDishImage() так и не доходит, так как я получаю ошибку:
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public org.springframework.http.ResponseEntity<com.example.internet_market_backend.entity.DishEntity> com.example.internet_market_backend.controller.DishController.updateDishImage(java.lang.Long,java.lang.String)]
Кто сталкивался, подскажите, в чем проблема?