return 类型未知 属性

Unknown Property in a return type

我正在尝试在我的 Play 2.4 Java8 JPA 项目中使用 mapstruct。我完成的步骤:

添加依赖项

  "org.mapstruct" % "mapstruct-jdk8" % "1.1.0.Beta1",
  "org.mapstruct" % "mapstruct-processor" % "1.1.0.Beta1"

型号

@Entity
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;            
    private String fullName;          
    private String email;
}

EmployeeDto

public class EmployeeDto {

    private String full_name;
    private String email;
}

EmployeeMapper

@Mapper
public interface EmployeeMapper {

    EmployeeMapper INSTANCE = Mappers.getMapper(EmployeeMapper.class);

    @Mapping(source = "fullName", target = "full_name")
    EmployeeDto employeeToEmployeeDto(Employee employee);
}

但是它给我一个编译错误

 error: Unknown property "full_name" in return type.
[error]     @Mapping(source = "fullName", target = "full_name")

错误可能是什么问题?

目标端的 bean 需要具有映射属性的设置器。

MapStruct 不使用反射来获取或设置映射类型中的状态,在生成的代码中使用纯 getter/setter 调用将状态从源传播到目标。

其他开发人员的附加案例:如果您使用 Lombok,maven 仅使用 MapStruct 处理器。所以 Lombok 无法生成 getters/setters。要解决这个问题,请在 annotationProcessorPaths.

中添加 lombok 依赖项

此外,如果您使用的是 Lombok 1.8.16 及更高版本,则还必须添加 lombok-mapstruct-binding

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>${compiler-plugin.version}</version>
    <configuration>
        <source>${java.version}</source>
        <target>${java.version}</target>
        <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>${projectlombok.version}</version>
            </path>
            <!-- This is needed when using Lombok 1.8.16 and above -->
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok-mapstruct-binding</artifactId>
                <version>0.1.0</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

兄弟姐妹们好

在我的例子中,要同时使用 Mapstruct(0.2.0) 和 Lombok(1.18.16),下面的块应该首先出现或在三个 'path' 块的中间。 我的意思是 1)mapstruct、2)lombok-mapstruct-binding 和 3)lombok 或 1)lombok-mapstruct-binding、2)mapstruct 和 3)lombok 对我有用。

<path>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok-mapstruct-binding</artifactId>
   <version>0.2.0</version>
</path>

来自https://mapstruct.org/faq/

的相关信息

Can I use MapStruct together with Project Lombok?

Yes, as of MapStruct 1.2.0.Beta1 and Lombok 1.16.14.

Project Lombok is an annotation processor that (amongst other things) adds getters and setters to the AST (abstract syntax tree) of compiled bean classes. AST modifications are not foreseen by Java annotation processing API, so quite some trickery was required within Lombok as well MapStruct to make both of them work together. Essentially, MapStruct will wait until Lombok has done all its amendments before generating mapper classes for Lombok-enhanced beans.

An example for using the two projects together can be found here.

If you are using Lombok 1.18.16 or newer you also need to add lombok-mapstruct-binding in order to make Lombok and MapStruct work together.

If you are on an older version of MapStruct or Lombok, the solution is to put the JavaBeans to be amended by Lombok and the mapper interfaces to be processed by MapStruct into two separate modules of your project. Then Lombok will run in the compilation of the first module, causing the bean classes to be complete when MapStruct runs during the compilation of the second module.