Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use MapStruct for different data types?

I have two types of data that I want to map:

SignUpUserDto:

public class SignUpUserDto {
    private String firstName;
    private String lastName;
    private String username;
    private String email;
    private String password;
    private String title;
}

SignUpUser:

@Entity
public class SignUpUser {
    private Long id;
    private String firstName;
    private String lastName;
    private String username;
    private String email;
    private String password;
    private Title title;
}

Title:

public enum Title {
    JUNIOR("junior"),
    MIDDLE("middle"),
    SENIOR("senior"),
    MANAGER("manager");

    private final String title;

    Title(final String title) {
        this.title = title;
    }

    public String toString() {
        return this.title;
    }
}
  • For DTO title member is a String.

  • For entity title member is a Title.

How should the mapper looks like?

Should I pass title already converted in Service?

@Mapper(componentModel = "spring")
public interface SignUpUserMapper {
    SignUpUserMapper INSTANCE = Mappers.getMapper(SignUpUserMapper.class);
    @Mapping(target = "title", expression = "title")
    public SignUpUserDto signUpUserToSignUpUserDto(SignUpUser signUpUser, String title);
    @Mapping(target = "title", source = "title")
    public SignUpUser signUpUserDtoToSignUpUser(SignUpUserDto signUpUserDto, Title title);
}

Or should I do conversion in Mapper?

@Mapper(componentModel = "spring",  imports = Title.class)
public interface SignUpUserMapper {
    SignUpUserMapper INSTANCE = Mappers.getMapper(SignUpUserMapper.class);
    @Mapping(target = "title", expression = "java(signUpUser.getTitle().toString())")
    public SignUpUserDto signUpUserToSignUpUserDto(SignUpUser signUpUser);
    @Mapping(target = "title", source = "java(new Title(signUpUserDto.getTitle()))")
    public SignUpUser signUpUserDtoToSignUpUser(SignUpUserDto signUpUserDto);
}
like image 483
Robert Avatar asked Aug 01 '19 09:08

Robert


2 Answers

Should I pass title already converted in Service?

You definitely shoul NOT do it. It is converter's job, not service's

Try following approach:

1) Add conversion method to enum class

enum Title {
    ...

    public static Title fromString(String title) {
        if (title != null) {
            for (Title t : Title.values()) {
                if (t.toString().equals(title)) {
                    return t;
                }
            }
        }
        return null;
    }
}

2) Add 2 conversion methods to Mapper interface (Java 8+ only)

@Mapper(componentModel = "spring")
public interface SignUpUserMapper {
    SignUpUserDto signUpUserToSignUpUserDto(SignUpUser signUpUser);
    SignUpUser signUpUserDtoToSignUpUser(SignUpUserDto signUpUserDto);

    default String fromEnum(Title title) {
        return title == null ? null : title.toString();
    }

    default Title toEnum(String title) {
        return title == null ? null : Title.fromString(title);
    }
}
like image 95
Nikolai Shevchenko Avatar answered Sep 17 '22 23:09

Nikolai Shevchenko


Use the second option like this:

 @Mapper(componentModel = "spring",  imports = Title.class)                     
public interface SignUpUserMapper {
SignUpUserMapper INSTANCE = Mappers.getMapper(SignUpUserMapper.class);
@Mapping(target = "title", expression = "java(signUpUser.getTitle().toString())")
public SignUpUserDto signUpUserToSignUpUserDto(SignUpUser signUpUser);
@Mapping(target = "title", source = "java(Title.valueOf(signUpUserDto.getTitle().toUpperCase()))")
public SignUpUser signUpUserDtoToSignUpUser(SignUpUserDto signUpUserDto);            
}
like image 25
Daoud Shaheen Avatar answered Sep 19 '22 23:09

Daoud Shaheen