Reputation: 6331
// 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?
Upvotes: 9
Views: 15313
Reputation: 1685
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:
Upvotes: 6
Reputation: 3237
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);
}
Upvotes: -1
Reputation: 127
Just add the annotationProcessor "org.projectlombok:lombok-mapstruct-binding:${lombokMapstructBindingVersion}"
to the build.gradle and it will work.
Upvotes: 6