Springboot Mapstruct


A Data Transfer Object(DTO) is an object that is used to encapsulate data or domain model, and send it from one subsystem of an application to another. DTOs are most commonly used by the Services layer in an N-Tier application to transfer data between itself and the UI layer.Mapstruct is by far the best DTO generator I have worked with for many reasons. Here is the official site.

Installation with maven

            <version>3.5.1</version> <!-- or newer version -->
                <source>1.8</source> <!-- depending on your project -->
                <target>1.8</target> <!-- depending on your project -->
                    <!-- other annotation processors -->

Create a Mapper interface

@Mapper(componentModel = "spring", nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface CarMapper {
    CarMapper INSTANCE = Mappers.getMapper( CarMapper.class );
    @Mapping(source = "numberOfSeats", target = "seatCount")
    CarDto carToCarDto(Car car);



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.
Car car = new Car( "Morris", 5, CarType.SEDAN );
CarDto carDto = CarMapper.INSTANCE.carToCarDto( car );



Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!


Leave a Reply

Your email address will not be published. Required fields are marked *