-1

I got the following two classes (I'm trying to map from 1st to 2nd):

@Data
@AllArgsConstructor
@NoArgsConstructor
public class RegisterRequestDto {

    private String username;
    private String firstName;
    private String lastName;
    private String password;

}


@Entity
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "users")
public class User extends BaseEntity {

    @Column(name = "username")
    private String username;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @Column(name = "password")
    private String password;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
            name = "user_roles",
            joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id")}
    )
    private List<Role> roles;

}

I also got the following mapper:

@Mapper(componentModel = "spring")
public interface RegisterRequestDtoToUser {

    User requestToUser(RegisterRequestDto requestDto);

}

This is what I got in my build.gradle file:

implementation 'org.mapstruct:mapstruct:1.4.2.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final'

And this is what mapper I get from mapstruct for classes I mentioned above, as you can see it's obviously invalid:

@Component
public class RegisterRequestDtoToUserImpl implements RegisterRequestDtoToUser {

    @Override
    public User requestToUser(RegisterRequestDto requestDto) {
        if ( requestDto == null ) {
            return null;
        }

        User user = new User();

        return user;
    }
}

What's wrong, how do I fix it?

nevermindhf
  • 19
  • 1
  • 5
  • What's the Lombok version you are using because since Lombok version 1.18.16, we also have to add the dependency lombok-mapstruct-binding. – Abhinav Pandey May 23 '22 at 11:25
  • compileOnly 'org.projectlombok:lombok:1.18.24' annotationProcessor 'org.projectlombok:lombok:1.18.24' – nevermindhf May 23 '22 at 11:27
  • Do you have [this dependency](https://mvnrepository.com/artifact/org.projectlombok/lombok-mapstruct-binding/0.2.0) added? - without it, Mapstruct and Lombok won't work together. – Abhinav Pandey May 23 '22 at 11:30
  • I added the following: implementation 'org.projectlombok:lombok-mapstruct-binding:0.2.0' Generated mapper is the same – nevermindhf May 23 '22 at 11:30

1 Answers1

1

After adding the following dependencies everything worked:

implementation "org.mapstruct:mapstruct:${mapstructVersion}", "org.projectlombok:lombok:${lombokVersion}"

annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}", "org.projectlombok:lombok:${lombokVersion}", "org.projectlombok:lombok-mapstruct-binding:${lombokMapstructBindingVersion}"
nevermindhf
  • 19
  • 1
  • 5