org.mapstruct don’t work with a base class

Tags: , , , ,



I have 2 classes. My base class:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class BaseRequest {

   @NotNull
   @JsonProperty("dateTransaction")
   private String dateTransaction;

   @NotNull
   @JsonProperty("hourTransaction")
   private String hourTransaction;

}

And my other class. This class extends BaseRequest class.

public class AddClass extends BaseRequest {

   @Setter
   @Getter
   private String additionalData60;

}

And When I try create a mapper to cast my AddClass to OtherClass:

@Component
@Mapper(componentModel = "spring", uses = {})
public interface OtherMapper{

   @Mappings({
        @Mapping(source = "hourTransaction", target = "timeTransaction"),
        @Mapping(source = "dateTransaction", target = "dateTransaction"),
        @Mapping(source = "additionalData60", target = "additionalData60"),
   })
   OtherClass toOtherClass(AddClass addClass);

}

When I run my build I received this error:

Error:(22,13) java: No property named “dateTransaction” exists in source parameter(s). Did you mean “null”?

Answer

MapStruct works without issues with Lombok.

However, if you have upgraded to Lombok 1.18.16 as per the Lombok Changelog then you need to add

lombok-mapstruct-binding

In Gradle:

annotationProcessor "org.projectlombok:lombok-mapstruct-binding:0.1.0"
<path>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok-mapstruct-binding</artifactId>
    <version>0.1.0</version>
</path>


Source: stackoverflow