MapStruct mapper error: Unknown property "propertyName" in result type Dto. Did you mean "null"?
Asked Answered
R

3

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

Resignation answered 4/8, 2022 at 17:26 Comment(0)
T
5

Just add the annotationProcessor "org.projectlombok:lombok-mapstruct-binding:${lombokMapstructBindingVersion}" to the build.gradle and it will work.

Testes answered 29/5, 2023 at 9:58 Comment(0)
D
3

It is related to Lombok.
Make sure to have added annotationProcessorPaths to maven-compiler-plugin, (order of paths matter)

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-compiler-plugin</artifactId>
     <version>3.8.0</version>
     <configuration>
         <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>${lombok.version}</version>
             </path>
             <dependency>
                 <groupId>org.projectlombok</groupId>
                 <artifactId>lombok-mapstruct-binding</artifactId>
                 <version>0.2.0</version>
             </dependency>
         </annotationProcessorPaths>
         <compilerArgs>
             <compilerArg>
                 -Amapstruct.defaultComponentModel=spring
             </compilerArg>
         </compilerArgs>
     </configuration>
 </plugin>

Take a look at:

Disposal answered 16/5 at 10:51 Comment(0)
I
-1

For someone looking to fix a more simpler problem could just add some getters and setters in a DTO.

In my case this helped.

You should pay attention to the Capital letters, I find it bit counter intuitive but, anyway some convention has to be there for the mapping

My DTO

public class SalesOrderDTO {

    private Integer SOID;
    private LocalDate SOOrderDate;

    private String CustomerName;
    private String CustomerID;

    private LocalDate SODueDate;

    private Boolean SOIsClosed;

    private LocalDateTime SOLastModified;


    public Integer getSOID() {
        return SOID;
    }

    public void setSOID(Integer SOID) {
        this.SOID = SOID;
    }

    public LocalDate getSOOrderDate() {
        return SOOrderDate;
    }

    public void setSOOrderDate(LocalDate SOOrderDate) {
        this.SOOrderDate = SOOrderDate;
    }

    public LocalDate getSODueDate() {
        return SODueDate;
    }

    public void setSODueDate(LocalDate SODueDate) {
        this.SODueDate = SODueDate;
    }

    public Boolean getSOIsClosed() {
        return SOIsClosed;
    }

    public void setSOIsClosed(Boolean SOIsClosed) {
        this.SOIsClosed = SOIsClosed;
    }

    public LocalDateTime getSOLastModified() {
        return SOLastModified;
    }

    public void setSOLastModified(LocalDateTime SOLastModified) {
        this.SOLastModified = SOLastModified;
    }

    public String getCustomerName() {
        return CustomerName;
    }

    public void setCustomerName(String customerName) {
        CustomerName = customerName;
    }

    public String getCustomerID() {
        return CustomerID;
    }

    public void setCustomerID(String customerID) {
        CustomerID = customerID;
    }
}

My Mapper

@Mapper(
        componentModel = "spring",
        unmappedTargetPolicy = ReportingPolicy.ERROR
)
public interface SalesOrderMapper {
    @Mapping(source = "customer.fullName", target = "customerName")
    @Mapping(source = "customer.NCCustomerID", target = "customerID")
    SalesOrderDTO toDto(SalesOrder salesOrder);
}
Indign answered 25/9, 2023 at 13:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.