Mapstruct 看不到相关的映射器

Mapstruct not see related mapper

我有一个映射器 UserMapper,它映射过滤器以供进一步选择数据库。

@Mapper(uses = {RoleMapper.class})
public interface UserMapper {
    @Mapping(source = "roleIds", target = "roles")
    UserFilter toUserFilter(UserFilterDto dto);
}

@Mapper
public interface RoleMapper {
default List<Role> roleIdListToRoleList(List<Long> roleIds) {
    return Objects.isNull(roleIds)
            ? Lists.newArrayList()
            : roleIds.stream().map(id -> Role.builder().id(id).build()).collect(Collectors.toList());
}
    }
}

来源 DTO

@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@JsonIgnoreProperties(ignoreUnknown = true)
public class UserFilterDto extends FilterDto {

    @JsonProperty("username")
    private String username;

    @JsonProperty("isEnabled")
    private Boolean isEnabled;

    @JsonProperty("roleIds")
    private List<Long> roleIds;
}

目标对象

@Data
@Builder
class UserFilter {

    private String username;

    private Boolean isEnabled;

    private List<Role> roles;
}

我希望每个 List<roleIds> 都是 List<Role>RoleMapper 的映射,并且 mapstruct 生成如下内容:

public UserFilter toUserFilter(UserFilterDto dto) {
    UserFilterBuilder userFilter = UserFilter.builder();
    userFilter.roles( roleMapper.roleIdListToRoleList(dto.getRoleIds()));
    userFilter.username( dto.getUsername() );
    userFilter.isEnabled( dto.getIsEnabled() );
    return userFilter.build();
}

但是,我得到一个编译错误:

No property named "roleIds" exists in source parameter(s). Did you mean "null"?

为什么会这样,如何解决?

更新 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.tradeshare</groupId>
    <artifactId>tradeshare</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>tradeshare</name>
    <description>tradeshare</description>
    <properties>
        <java.version>11</java.version>
        <jsonwebtoken.version>0.9.1</jsonwebtoken.version>
        <mapstruct.version>1.4.1.Final</mapstruct.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jetbrains</groupId>
            <artifactId>annotations</artifactId>
            <version>13.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>net.minidev</groupId>
            <artifactId>json-smart</artifactId>
            <version>2.4.7</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>30.1.1-jre</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${mapstruct.version}</version>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>${jsonwebtoken.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${mapstruct.version}</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                    </annotationProcessorPaths>
                    <compilerArgs>
                        <compilerArg>-Amapstruct.defaultComponentModel=spring</compilerArg>
                    </compilerArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

您似乎使用的是 Lombok v. > 1.18。如果是这样,你就错过了

lombok-mapstruct-binding

在 pom 中添加下一个依赖项:

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

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

在注释处理器中应该修复您的 mapstruct 错误。

Mapstruct Lombok doc