Home > database >  MapStruct mapper error: Unknown property "propertyName" in result type Dto. Did you mean &
MapStruct mapper error: Unknown property "propertyName" in result type Dto. Did you mean &

Time:08-05

// 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?

CodePudding user response:

Solution 1:

It can be related to Lombok.

Take a look at:

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)
  • Related