简体   繁体   中英

MapStruct: How can I do filter to fields which I want and don't want to mapping?

I have a DTO. I want to map not all field in MapStruct.

For example, User and UserDTO

public class UserDTO {

    private Long id;
    private String username;
    private String password;
    private String email;
    private boolean active;
    private String activationCode;
    private Set<Role> roles;

}

I have next mapper:

User fromUserDTO(UserDTO userDTO);

I don't want to map id, username, password, email. How can I point out that these fields don't get stuck?

User user = fromUserDTO(userDTO);

I've found the answer.

  1. I can ignore a field:
@Mapping(target = "id", ignore = true)
User fromUserDTO(UserDTO userDTO);
  1. Or I can ignore a field which equals null:
@Mapping(target="id", nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
User fromUserDTO(UserDTO userDTO);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM