No read accessor found for property in target type

Problem: When you are trying to map nested objects and you are using Mapstruct and Lombok and it returns next error:

no read accessor found for property in target type

One solution: Just remove @Builder in the class where the property is causing the problem to avoid the conflict.

Other solution:

If you don’t want to remove @Builder, try to split the mapping. Example:

class CarDto {
 String color;
 int inchesOfWheels;
 String brandOfWheels;
}

class Car {
 String color;
 Wheels wheels;
}

class Wheels {
 int inchesOfWheels;
 String brandOfWheels;
}
// It doesn't work.
@Mapping( target = "wheels.inchesOfWheels", source = "inchesOfWheels" )
@Mapping( target = "wheels.brandOfWheels", source = "brandOfWheels" )
Car map(CarDto carDto);

// It works
@Mapping( target = "wheels", source = "." )
Car mapCarDtoToCar(CarDto carDto);

@Mapping( target = "inchesOfWheels", source = "inchesOfWheels" )
@Mapping( target = "brandOfWheels", source = "brandOfWheels" )
Wheels mapCarDtoToWheels(CarDto carDto);

For more interesting tutorials & guides just check them HERE.

Leave a Reply

Your email address will not be published. Required fields are marked *