1
votes

Im trying to use mapstruct to convert a DTO to my class representation.

The class looks something like follows:

public class Loan{
    private Amount total;
    private Amount paid;
}

public class Amount{
    private Long amount;
    private String currency;
}

The DTO class would be something like:

public class LoanDTO{
    private Long paidAmount;
    private Long totalAmount;
    private String currency;
}

My mapper function would be something like this but I am unsure how I can get 'Amount' mapped properly.

@Mapper
public interface ResposeMap {

    Loan toLoan(LoanDTO loanDTO);
}

1

1 Answers

1
votes

If you use java8 and mapstruct 1.3(maybe it's work in earlier versions) then it can be so:

@Mapper
public interface ResposeMap {

    @Mapping(target = "total.amount", source = "totalAmount")
    @Mapping(target = "total.currency", source = "currency")
    @Mapping(target = "paid.amount", source = "paidAmount")
    @Mapping(target = "paid.currency", source = "currency")
    Loan toLoan(LoanDTO loanDTO);
}

more about it: Mapstruct documentation