title: Spring boot builder pattern entity to dto
body: <p>I'm creating an API that transmits data from another web service. I'm looking for good practice to convert the objects from the web service to my DTO's. The conversion from the object to DTO has some logic.</p>
  <p>I have now implemented this (pseudo-code):</p>
  <p>I have a main builder that contains generic code that the other builders can use.</p>
  <pre><code>@Component
  public abstract class RelationDtoBuilder &lt;T extends RelationDtoBuilder &lt;T&gt;&gt; {
  protected abstract T getThis();
  
  @Autowired
  private PhoneNumberDtoBuilder phoneNumberDtoBuilder ;
  
  protected List&lt;PhoneNumberDto&gt; phoneNumbers= new ArrayList&lt;&gt;();
  
  public T setPhoneNumbers(List&lt;PhoneNumber&gt; phoneNumbers) {
      if (phoneNumbers!= null) {
          phoneNumbers.stream().forEach(phoneNumber -&gt; {
              this.phoneNumbers.add(
                      phoneNumberDtoBuilder 
                              .setNumber(branch.getNumber())
                              .setType(branch.getType())
                              .build()
              );            });
      }
      return getThis();
  }
  
  }
  </code></pre>
  <p>Subclass builder for a PersonDto</p>
  <pre><code>@Component
  public final class PersonDtoBuilder extends RelationDtoBuilder&lt;PersonDtoBuilder&gt; {
  
  @Override
  protected PersonDtoBuilder getThis() {
      return this;
  }
  
  public PersonDto build() {
      PersonDto personDto = new PersonDto();
      PersonDto.setPhoneNumbers(super.phoneNumbers);
      return PersonDto;
  }
  
  }
  </code></pre>
  <p>Subclass builder for a CompanyDto</p>
  <pre><code>@Component
  public final class CompanyDtoBuilder extends RelationDtoBuilder&lt;CompanyDtoBuilder &gt; {
  
  protected List&lt;BranchDto&gt; branches = new ArrayList&lt;&gt;();
  
  @Override
  protected CompanyDtoBuilder getThis() {
      return this;
  }
  
  public CompanyDtoBuilder setBranches(List&lt;Branch&gt; branches) {
      if (branches != null) {
          branches.stream().forEach(branch -&gt; {
              this.branches.add(
                      branchDtoBuilder
                              .setName(branch.getName())
                              .setType(branch.getType())
                              .setPhoneNumbers(branch.getPhoneNumbers)
                              .build()
              );
          });          
      }
      return getThis();
  }
  
  
  public CompanyDtoBuilder build() {
      CompanyDto companyDto= new CompanyDto ();
      companyDto.setPhoneNumbers(super.phoneNumbers);
      companyDto.setBranches(this.branches);
      return CompanyDto;
  }
  
  
  }
  </code></pre>
  <p>BranchDtoBuilder</p>
  <pre><code>@Component
  public final class BranchDtoBuilder extends RelationDtoBuilder&lt;PersonDtoBuilder&gt;{
  
  @Autowired
  private ValueConverterService valueConverterService;
  
  private String name;
  private String type;
  
  @Override
  protected BranchDtoBuilder getThis() {
      return this;
  }
  
  public BranchDtoBuilder setName(String name) {
      this.name= name;
      return this;
  }
  
  public BranchDtoBuilder setType(String type) {
      if (type != null) {
          this.type= valueConverterService.convertValue(&quot;branchType&quot;, type);
      }
      return this;
  }
  
  public BranchDtoBuilder build() {
      BranchDto branchDto = new branchDto();
      branchDto.setSoort(this.soort);
      branchDto.setWaarde(this.waarde);
      branchDto.setPhoneNumbers(this.datumIngang);
      return branchDto ;
  }
  
  }
  </code></pre>
  <p>Problem is that de BranchDtoBuilder is using the same reference for phone numbers as the CompanyDtoBuilder. How can I achieve constructing the DTO's without having duplicate code?</p>
  <p>Secondly, is this the best way to map entities to DTO's with logic as database conversions (BranchDtoBuilder)?</p>