CachePut how to get and Id of returned value?

@CachePut(value = "dishDTOs", key = "IDOFTHERETURNEDVALUE")
@CacheEvict(cacheNames = "dishDTOList", allEntries = true)
public DishResponseDTO createDish(DishCreationDTO dishCreationDTO) {
    int restaurantId = dishCreationDTO.getRestaurantId();
    var restaurant = restaurantRepository.getById(restaurantId);

    var dish = DishMapper.toDish(dishCreationDTO, restaurant);
    return toDishResponseDTO(dishRepository.save(dish));
}

Id is assigned by the DB so I need to get it somehow to put as a key.

Answer

The returned object can be accessed by SpeL with #result and you can access object params with a . and field name. For example

@CachePut(value = "dishDTOs", key = "#result.id)
@CacheEvict(cacheNames = "dishDTOList", allEntries = true)
public DishResponseDTO createDish(DishCreationDTO dishCreationDTO) {
    int restaurantId = dishCreationDTO.getRestaurantId();
    var restaurant = restaurantRepository.getById(restaurantId);

    var dish = DishMapper.toDish(dishCreationDTO, restaurant);
    return toDishResponseDTO(dishRepository.save(dish));
}

Change .id with your DishResponseDTO field name.