【问题标题】:MapStruct can't find property both in source and targetMapStruct 在源和目标中都找不到属性
【发布时间】:2020-09-25 13:54:14
【问题描述】:

源类看起来像:

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"?

请注意,我将 MapStruct 与 Lombok 一起使用。这里有任何与预处理器相关的问题吗?

【问题讨论】:

  • delomboked 类的外观如何?有普通的吸气剂还是流利的吸气剂?
  • setter 已启用链接。
  • 吸气剂呢?他们看起来怎么样?我不是 Lombok 用户,所以我不熟悉他们的 API
  • 你的 pom.xml 看起来怎么样?

标签: spring mapstruct annotation-processing mapper


【解决方案1】:

在我的 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>

【讨论】:

    猜你喜欢
    • 2021-12-12
    • 2022-11-09
    • 2021-10-03
    • 2020-03-04
    • 1970-01-01
    • 2018-04-09
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    相关资源
    最近更新 更多