title: Best way to mapping dto/domain for transport between microservices
body: <p>I have DTO and DOMAIN models:</p>
  
  <pre><code>@Data
  public class CarDomain {
      private String name;
      private int year;
      private String color;
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>@Data
  public class CarDto {
      private String name;
      private int year;
      private String color;
  }
  </code></pre>
  
  <p>I have 3 microservices(MS) communicate with each other through RabbitMq.
  And I have <code>models module</code> with all DTO classes. Each MS include <code>models module</code>
  in maven.</p>
  
  <p>1 MS send carDto 2 MS recive CarDto and convert to Domain. For this I can use Several variants:</p>
  
  <ol>
  <li>use library for example <code>mapstruct</code>:</li>
  </ol>
  
  <blockquote>
  <pre><code>@Mapper
  public interface CarMapper {
      CarMapper INSTANCE = Mappers.getMapper(CarMapper.class );
      CarDto carToCarDto(CarDomain car);
  }
  </code></pre>
  </blockquote>
  
  <p>and use:</p>
  
  <pre><code>CarDto carDto = CarMapper.INSTANCE.carToCarDto(carDomain);
  </code></pre>
  
  <ol start="2">
  <li>Create manual mapper:</li>
  </ol>
  
  <blockquote>
  <pre><code>class CarMapper {
          public static CarDto toDto(CarDomain car) {
              CarDto carDto = new CarDto();
              carDto.setName(car.getName());
              carDto.setYear(car.getYear());
              carDto.setColor(car.getColor());
          }
      }
  </code></pre>
  </blockquote>
  
  <p>Now we use 2 variant. Becouse when we build microservice and in <code>models module</code> some field of DTO model change we get error on compile time. For example somebody change name this dto model in <code>models module</code></p>
  
  <pre><code>private String name; 
  </code></pre>
  
  <p>to </p>
  
  <pre><code>private String name1;
  </code></pre>
  
  <p>When we build project we get error on this line:</p>
  
  <pre><code>carDto.setName(car.getName());// getName not found becose now getName1
  </code></pre>
  
  <p>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.</p>
  
  <p>Tell me the best approach how to match/mapping models dto/domain?</p>