MapStruct: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
No edit summary
Line 84: Line 84:
* [https://plugins.jetbrains.com/plugin/10036-mapstruct-support IntelliJ IDEA MapStruct Support]
* [https://plugins.jetbrains.com/plugin/10036-mapstruct-support IntelliJ IDEA MapStruct Support]
* [https://www.baeldung.com/mapstruct Quick Guide to MapStruct]
* [https://www.baeldung.com/mapstruct Quick Guide to MapStruct]
* [https://mapstruct.org/news/2019-12-06-mapstruct-and-quarkus/ Quarkus MapStruct]
* [https://mapstruct.org/ MapStruct]
* [https://mapstruct.org/ MapStruct]
* [[Lombok]]
* [[Lombok]]

Revision as of 08:36, 23 September 2021

public class Car {
    private String make;
    private int numberOfSeats;
    private CarType type;
 
    //constructor, getters, setters etc.
}
public class CarDto {
    private String make;
    private int seatCount;
    private String type;
 
    //constructor, getters, setters etc.
}
@Mapper
public interface CarMapper {
    CarMapper INSTANCE = Mappers.getMapper(CarMapper.class);
 
    @Mapping(source = "numberOfSeats", target = "seatCount")
    CarDto toDto(Car car);
}
@Test
public void shouldMapCarToDto() {
    Car    car = new Car("Morris", 5, CarType.SEDAN);
    CarDto dto = CarMapper.INSTANCE.toDto(car);
 
    //then
    assertThat(dto).isNotNull();
    assertThat(dto.getMake()).isEqualTo("Morris");
    assertThat(dto.getSeatCount()).isEqualTo(5);
    assertThat(dto.getType()).isEqualTo("SEDAN");
}

Maven Plugins

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <annotationProcessorPaths>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>1.4.2.Final</version>
            </path>
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.4</version>
            </path>
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok-mapstruct-binding</artifactId>
                <version>0.2.0</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

References