Monday, July 5, 2021

Quick Summary on Lombok and MapStruct

 

Lombok

@Data

@AllArgsConstructor

@RequiredArgsConstructor

@NoArgsConstructor

@Getter

@Setter

@Builder


And Special 


@Singular

@NonNull

@Cleanup

@Value

@SneakyThrows

@Log

@With



MapStruct


Multi-layered applications often require to map between different object models (e.g. entities and DTOs). Writing such mapping code is a tedious and error-prone task. MapStruct aims at simplifying this work by automating it as much as possible.

In contrast to other mapping frameworks MapStruct generates bean mappings at compile-time which ensures a high performance, allows for fast developer feedback and thorough error checking.



  1. public class Car {
  2.  
  3. private String make;
  4. private int numberOfSeats;
  5. private CarType type;
  6.  
  7. //constructor, getters, setters etc.
  8. }


  1. public class CarDto {
  2.  
  3. private String make;
  4. private int seatCount;
  5. private String type;
  6.  
  7. //constructor, getters, setters etc.
  8. }


Here we define the Mapper. So after compilation the carToCarDto method is implemented as simple java code.

  1. @Mapper 1
  2. public interface CarMapper {
  3.  
  4. CarMapper INSTANCE = Mappers.getMapper( CarMapper.class ); 3
  5.  
  6. @Mapping(source = "numberOfSeats", target = "seatCount")
  7. CarDto carToCarDto(Car car); 2
  8. }




How to add AnnotationProcessor in gradle.


https://tomgregory.com/annotation-processors-in-gradle-with-the-annotationprocessor-dependency-configuration/