javaspringmapstructobject-object-mapping

Multiple problems regarding MapStruct mapping


I am new to using MapStruct and thus facing some issues with the same.

I have the following Model classes :-

@Data
class User {

@Field
private String fullName;

@Field("experience")
private List<Experience> workExperience;

//other fields

}

@Data
class Experience {

private Date joiningDate;

//other fields
}

Now, I have the following DTO's

@Data
class UserDTO {

 private String firstName;

 private String lastName;

 private List<ExperienceDTO> workExperience;

 //other fields

}

@Data
class ExperienceDTO {

private String joiningDate;

//other fields
}

Have written the UserMapper Interface as :-

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

 @Mappings({
            @Mapping(target = "firstName",source = "fullName",
                    qualifiedByName = "firstNameExtractor"),
            @Mapping(target = "lastName",source = "fullName",
                    qualifiedByName = "lastNameExtractor")
    })
    UserDTO userToUserDTO(User user);

  @Mappings({
      @Mapping(target = "joiningDate", source = "joiningDate",
              dateFormat = "yyyy-MM-dd HH:mm:ss")
    })
    List<ExperienceDTO> experienceToExperienceDTO(List<Experience> experience);

@Named("firstNameExtractor")
    public static String getFirstName(String name){
        String[] nameParts = name.split(" ");
        return nameParts[0];
    }
//similarly have a lastNameExtractor

But I get the following errors :-

  1. No property named "fullName" exists in source parameter(s). Did you mean "null"?
  2. Unknown property "firstName" in result type com.personal.portfolio.dto.UserDTO. Did you mean "null"?
  3. No property named "joiningDate" exists in source parameter(s). Did you mean empty"?
  4. Unknown property "joiningDate" in result type java.util.List. Did you mean "empty"?

I know my design might be wrong, but I am intentionally doing it this way to understand how MapStruct works. Kindly anyone could help me to understand what mistake I am doing?


Solution

  • The first error is because MapStruct does not see Lombok annotated methods. Add the annotation processor to the build:

                       <annotationProcessorPaths>
                            <path>
                                <groupId>org.mapstruct</groupId>
                                <artifactId>mapstruct-processor</artifactId>
                                <version>${org.mapstruct.version}</version>
                            </path>
                            <path>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                                <version>${org.projectlombok.version}</version>
                            </path>
                        </annotationProcessorPaths>
    

    The second is that you add @Mapping to the list of objects, not the object itself. Create a method, which map a single Experience to ExperienceDTO, add the annotation there and remove it from the experienceToExperienceDTO method.