Mapstruct问题:未知属性错误

aly*_*oon 1 gradle annotation-processing lombok mapstruct

我一起使用 mapstruct 和 lombok 并遇到了一些问题:

EntityMapper.java:10: error: Unknown property "id" in result type Entity. Did you mean "null"?
    @Mapping(target = "id", ignore = true)
                      ^
Run Code Online (Sandbox Code Playgroud)

我的 Entity 和 EntityDto 类:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Entity {

    private int id;

    private String property;
}

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class EntityDto {

    private String property;
}
Run Code Online (Sandbox Code Playgroud)

实体映射器:

@Mapper(implementationName = "MapStruct<CLASS_NAME>")
public interface EntityMapper {

    // neither of them work
    @Mapping(target = "id", ignore = true)
    //@Mapping(target = "id", defaultValue = "0")
    Entity map(EntityDto dto);

    EntityDto map(Entity entity);

}
Run Code Online (Sandbox Code Playgroud)

在此配置中,它会导致编译时错误。所以我尝试注释掉@Mapping注释。它已编译,但将所有属性映射为 null。MapStructEntityMapper 生成的实现:

public class MapStructEntityMapper implements EntityMapper {
    public MapStructEntityMapper() {
    }

    public Entity map(EntityDto dto) {
        if (dto == null) {
            return null;
        } else {
            Entity entity = new Entity();
            return entity;
        }
    }

    public EntityDto map(Entity entity) {
        if (entity == null) {
            return null;
        } else {
            EntityDto entityDto = new EntityDto();
            return entityDto;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我找到了几个关于注释处理器的答案,但请看一下我的 build.gradle 文件:

// MapStruct - Entity-DTO mapper                             
implementation 'org.mapstruct:mapstruct:1.4.1.Final'         
annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.1.
compile 'org.projectlombok:lombok-mapstruct-binding:0.1.0'   
// Util                                                      
// lombok                                                    
compileOnly 'org.projectlombok:lombok:1.18.16'               
annotationProcessor 'org.projectlombok:lombok:1.18.16'       
testCompileOnly 'org.projectlombok:lombok:1.18.16'           
testAnnotationProcessor 'org.projectlombok:lombok:1.18.16'   
Run Code Online (Sandbox Code Playgroud)

有时,如果我在没有 @Mapping 注释的情况下进行编译,然后使用此注释运行,它会起作用,但现在即使这样也不起作用。

Fil*_*lip 5

这似乎是 的问题lombok-mapstruct-binding

这应该与处理器处于同一范围内。你需要把它放在下面annotationProcessor而不是compile