Mapstruct 问题:未知 属性 错误

Mapstruct problem: Unknown property error

我同时使用 mapstruct 和 lombok,遇到了一些问题:

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

我的实体和 EntityDto 类:

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

    private int id;

    private String property;
}

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

    private String property;
}

实体映射器:

@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);

}

在这个配置中它会导致编译时错误。所以我试图注释掉@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;
        }
    }
}

我找到了几个关于注释处理器的答案,但请看一下我的 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'   

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

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

这应该与处理器在同一范围内。您需要将其放在 annotationProcessor 而不是 compile