Issue
// Driver model
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Driver {
private String driverName;
private String licenseNumber;
}
// Car model
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Car {
private String make;
private List<Driver> drivers;
private CarType type;
}
// Car DTO
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CarDto {
private String make;
private Integer totalDrivers;
private String type;
}
@Mapper
public interface CarMapper {
@Mapping(target = "totalDrivers", expression = "java(mapDrivers(car.getDrivers()))")
CarDto mapCarDto(Car car);
default Integer mapDrivers(List<Driver> totalDrivers) {
return totalDrivers.size();
}
@InheritInverseConfiguration
@Mapping(target = "drivers", ignore = true)
Car mapDtoToCar(CarDto carDto);
}
When I going to RUN this project those errors are reported:
..\CarMapper.java
java: Unknown property "totalDrivers" in result type CarDto. Did you mean "null"?
java: Unknown property "drivers" in result type Car. Did you mean "null"?
How can I get solve this problem?
Solution
Solution 1:
It can be related to Lombok.
Take a look at:
- Official documentation: Can I use MapStruct together with Project Lombok? and project example: github/mapstruct-examples/mapstruct-lombok/
- Guide step-by-step: Using Mapstruct With Project Lombok
- StackOverflow question: MapStruct and Lombok not working together
Solution 2:
I had a similar issue, for Object (or List of Object) that starts with the char "D". I solved it by using the name of the field with the first letter in uppercase as value of the target.
to solve: java: Unknown property "drivers" in result type Car. Did you mean "null"?
@Mapping(target = "Drivers", ignore = true)
Answered By - Paul Marcelin Bejan
Answer Checked By - Senaida (JavaFixing Volunteer)