Last active
February 25, 2023 09:49
-
-
Save susimsek/60a028d7166a21b4c65ac17909aef11c to your computer and use it in GitHub Desktop.
Spring Boot Mapstruct With Decorator Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @ApiModel(value = "Invoice Model") | |
| @Data | |
| @NoArgsConstructor | |
| @AllArgsConstructor | |
| @Builder | |
| public class InvoiceDto { | |
| String id; | |
| String taxId; | |
| String viewLink; | |
| String downloadLink; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Mapper | |
| @DecoratedWith(InvoiceMapperDecorator.class) | |
| public interface InvoiceMapper { | |
| InvoiceDto invoiceToInvoiceDto(Invoice invoice); | |
| InvoiceDto invoiceToInvoiceDtoWithLink(Invoice invoice); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good job