Skip to content

Instantly share code, notes, and snippets.

@susimsek
Last active February 25, 2023 09:49
Show Gist options
  • Select an option

  • Save susimsek/60a028d7166a21b4c65ac17909aef11c to your computer and use it in GitHub Desktop.

Select an option

Save susimsek/60a028d7166a21b4c65ac17909aef11c to your computer and use it in GitHub Desktop.
Spring Boot Mapstruct With Decorator Pattern
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@Entity
@FieldDefaults(level = AccessLevel.PRIVATE)
@Table(name = "invoices")
public class Invoice {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
String id;
@Column(length = 20)
String taxId;
}
@ApiModel(value = "Invoice Model")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class InvoiceDto {
String id;
String taxId;
String viewLink;
String downloadLink;
}
@Mapper
@DecoratedWith(InvoiceMapperDecorator.class)
public interface InvoiceMapper {
InvoiceDto invoiceToInvoiceDto(Invoice invoice);
InvoiceDto invoiceToInvoiceDtoWithLink(Invoice invoice);
}
@FieldDefaults(level = AccessLevel.PRIVATE)
public abstract class InvoiceMapperDecorator implements InvoiceMapper {
@Setter(onMethod=@__({@Autowired}))
InvoiceMapper invoiceMapper;
@Override
public InvoiceDto invoiceToInvoiceDto(Invoice invoice) {
return invoiceMapper.invoiceToInvoiceDto(invoice);
}
@Override
public InvoiceDto invoiceToInvoiceDtoWithLink(Invoice invoice) {
InvoiceDto dto = invoiceMapper.invoiceToInvoiceDto(invoice);
dto.setDownloadLink(getDownloadLink(invoice.getId()));
dto.setViewLink(getViewLink(invoice.getId()));
return dto;
}
public String getViewLink(String id) {
String url = MvcUriComponentsBuilder
.fromMethodName(InvoiceController.class, "viewInvoice", id)
.build().toString();
return url;
}
public String getDownloadLink(String id) {
String url = MvcUriComponentsBuilder
.fromMethodName(InvoiceController.class, "downloadInvoice",id)
.build().toString();
return url;
}
}
<properties>
<java.version>11</java.version>
<mapstruct.version>1.4.1.Final</mapstruct.version>
<lombok-mapstruct-binding.version>0.1.0</lombok-mapstruct-binding.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${lombok-mapstruct-binding.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<compilerArg>-Amapstruct.defaultComponentModel=spring</compilerArg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
@shavkat1121
Copy link

good job

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment