Friends,
Here are my Java objects
@Data
public class CarDto {
private String make;
private String model;
private int year;
}
@Data
public class Car {
private MakeEnum make;
private String model;
private int year;
}
For consuming, I need to do something like this
@Mapper
public interface CarMapper {
CarMapper INSTANCE = Mappers.getMapper(CarMapper.class);
Car toModel(CarDto carDto);
CarDto toDto(Car carModel);
}
// Using mapper
Car carModel = CarMapper.INSTANCE.toModel(carDto);
But I am looking a solution, where I could do this:
Car carModel = Mapper.map(carDto, Car.class);
How do do this? Didn't find an example where I can dynamically map based on a Type. I found this method very handy in both ModelMapper
& Google gson
. Appreciate your help!
If I understand you correctly, you require a kind of repo.
Another option would be to look at sprint and the new MapStruct spring integration, recently developed: https://github.com/mapstruct/mapstruct-spring-extensions. It was designed as a follow up of this question.
There's an example in the example repo. It's not straightforward though: https://github.com/mapstruct/mapstruct-examples/tree/master/mapstruct-mapper-repo