0
votes

Hello I have the following problem using MapStruct, I have the next interfaces:

public interface IRole {
    String getName();        
    void setName(String name);
}

public interface IUser {
    public String getUsername();
    public void setUsername(String username);
    public IRole getRole();
    public void setRole(IRole role);
    public String getPassword();
    public void setPassword(String password);
}

I have two implementations of both:

User,UserDto,Role and RoleDto.

I created a Mapper named UserMapper:

@Mapper
public interface UserMapper {

    UserDto userToUserDto(User user);

    RoleDto roleToRoleDto(Role user);

}

The problem is that when I try to transform a User to a UserDto, the role object is not transformed, I'm executing the next main method:

public static void main(String[] args) {
    UserMapper instance = Mappers.getMapper( UserMapper.class );
    Role r=new Role();
    r.setName("Admin");
    User user=new User();
    user.setUsername("Alex");
    user.setPassword("Raidentrance123");
    user.setRole(r);
    UserDto dto=instance.userToUserDto(user);
    System.out.println(dto.getUsername());      
    System.out.println(dto.getRole().getClass());       
}

And the result is :

Alex class com.raidentrance.model.Role

and I expected

Alex class com.raidentrance.model.RoleDto

Here is my question detailed Source and target share the same interface MapStruct

Here is my code I'm not sure if there is something like in Jackson the @JsonDeserialize(as=Impl.class) or in JPA the targetEntity

Note: I have a solution in the branch works in the github repository. There I changed the datatype for getRole from IRole getRole() to RoleDto getRole() and It works, but I'm looking for something like I mentioned.

1

1 Answers

1
votes

You must define a mapping method for the interface:

RoleDto roleToRoleDto(IRole role);

So mapstruct now can do a mapping, if a method returns an IRole-interface.