MapStruct在源和目标中均找不到属性

问题描述 投票:0回答:1

源类看起来像:

Data
@Accessors(chain = true)
@Validated
public class OAuth2ClientCreateRequest {

    @NotNull
    Data data;

    @lombok.Data
    @Accessors(chain = true)
    public static class Data {

        @Pattern(regexp = "oauth2_clients")
        private String type;

        @NotNull
        private OAuth2ClientAttributes attributes;
    }

    @lombok.Data
    @Accessors(chain = true)
    public static class OAuth2ClientAttributes {

        @NotNull @Length(min = 10, max = 256)
        private String clientId;
......

目标类如下:

@Accessors(chain = true)
@Getter
@Setter
@ToString
public class OAuth2Client extends BaseEntity<OAuth2Client> implements Serializable {

    @NotNull
    @Length(min = 10, max = 256)
    @JsonProperty
    private String clientId;

........

映射器类:

@Mapper(componentModel = "spring")
public interface OAuth2ClientMapper {

    @Mapping(target = "clientId", source = "attr.clientId")
    OAuth2Client convert(OAuth2ClientCreateRequest.OAuth2ClientAttributes attr);

}

我在执行Maven编译时遇到的错误:

[ERROR] ....../OAuth2ClientMapper.java:[14,52] The type of parameter "attr" has no property named "clientId".
[ERROR] ....../OAuth2ClientMapper.java:[14,52] Unknown property "clientId" in result type .....oauth2authserver.domain.entity.OAuth2Client. Did you mean "null"?

[注意,我在Lombok中使用MapStruct。这里有没有与预处理器有关的问题?

spring mapstruct annotation-processing mapper
1个回答
0
投票

在我的IntelliJ IDE项目中,Lombok在不添加任何注释预处理程序的情况下工作,因为Lombok插件是通过IntelliJ设置下载的。

然后,在pom.xml中添加map-struct依赖项时,我不得不在pom.xml中添加注释预处理器插件mapstruct-processor。然后Lombok开始无法工作。

最后为LombokMap-Struct都添加注释处理器,如下所示-

<properties>
    <java.version>11</java.version>
    <mapstruct.version>1.3.1.Final</mapstruct.version>
    <gson.version>2.8.5</gson.version>
</properties>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>${java.version}</source>
        <target>${java.version}</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>
    </configuration>
</plugin>
© www.soinside.com 2019 - 2024. All rights reserved.