I have Car:
And CarDTO:
In my service class I'm passing additional parameter "owner" and I need to convert the list.
Is it possible to add "owner" to Mapper?
If yes then I suppose it should be something similar to this (not working).
@Mapper
public interface CarMapper {
@Mapping(target = "owner", source = "owner")
List<Car> mapCars(List<CarDTO> cars, String owner);
}
As described in the answer, you can use @Context.
Firstly, add a single object mapping method:
@Maping(target = "owner", source = "owner")
Car mapCar(CarDTO car, String owner);
Then define a method for mapping a list of objects with @Context:
List<Car> mapCars(List<CarDTO> cars, @Context String owner);
Since
@Contextparameters are not meant to be used as source parameters, a proxy method should be added to point MapStruct to the right single object mapping method to make it work.
In the end, add the proxy method:
default Car mapContext(CarDTO car, @Context String owner) {
return mapCar(car, owner);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With