I have DTO and DOMAIN models:
@Data
public class CarDomain {
private String name;
private int year;
private String color;
}
and
@Data
public class CarDto {
private String name;
private int year;
private String color;
}
I have 3 microservices(MS) communicate with each other through RabbitMq.
And I have models module
with all DTO classes. Each MS include models module
in maven.
1 MS send carDto 2 MS recive CarDto and convert to Domain. For this I can use Several variants:
- use library for example
mapstruct
:
@Mapper public interface CarMapper { CarMapper INSTANCE = Mappers.getMapper(CarMapper.class ); CarDto carToCarDto(CarDomain car); }
and use:
CarDto carDto = CarMapper.INSTANCE.carToCarDto(carDomain);
- Create manual mapper:
class CarMapper { public static CarDto toDto(CarDomain car) { CarDto carDto = new CarDto(); carDto.setName(car.getName()); carDto.setYear(car.getYear()); carDto.setColor(car.getColor()); } }
Now we use 2 variant. Becouse when we build microservice and in models module
some field of DTO model change we get error on compile time. For example somebody change name this dto model in models module
private String name;
to
private String name1;
When we build project we get error on this line:
carDto.setName(car.getName());// getName not found becose now getName1
But this way hard. For each dto/domain models we need create mapper and write each field. In 1 variants it easier but if dto change we get error on runtime.
Tell me the best approach how to match/mapping models dto/domain?