org.mapstruct不与基类一起工作

huangapple go评论86阅读模式
英文:

org.mapstruct don't work with a base class

问题

我有2个类。我的基类:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class BaseRequest {

   @NotNull
   @JsonProperty("dateTransaction")
   private String dateTransaction;

   @NotNull
   @JsonProperty("hourTransaction")
   private String hourTransaction;

}

还有我的另一个类。这个类扩展了BaseRequest类。

public class AddClass extends BaseRequest {

   @Setter
   @Getter
   private String additionalData60;

}

当我尝试创建一个映射器来将我的AddClass转换为OtherClass时:

@Component
@Mapper(componentModel = "spring", uses = {})
public interface OtherMapper {

   @Mappings({
        @Mapping(source = "hourTransaction", target = "timeTransaction"),
        @Mapping(source = "dateTransaction", target = "dateTransaction"),
        @Mapping(source = "additionalData60", target = "additionalData60"),
   })
   OtherClass toOtherClass(AddClass addClass);

}

当我运行构建时,我收到了这个错误:

Error:(22,13) java: No property named "dateTransaction" exists in source parameter(s). Did you mean "null"?
英文:

I have 2 classes. My base class:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class BaseRequest {

   @NotNull
   @JsonProperty("dateTransaction")
   private String dateTransaction;

   @NotNull
   @JsonProperty("hourTransaction")
   private String hourTransaction;

}

And my other class. This class extends BaseRequest class.

public class AddClass extends BaseRequest {

   @Setter
   @Getter
   private String additionalData60;

}

And When I try create a mapper to cast my AddClass to OtherClass:

@Component
@Mapper(componentModel = "spring", uses = {})
public interface OtherMapper{

   @Mappings({
        @Mapping(source = "hourTransaction", target = "timeTransaction"),
        @Mapping(source = "dateTransaction", target = "dateTransaction"),
        @Mapping(source = "additionalData60", target = "additionalData60"),
   })
   OtherClass toOtherClass(AddClass addClass);

}

When I run my build I received this error:

> Error:(22,13) java: No property named "dateTransaction" exists in source parameter(s). Did you mean "null"?

答案1

得分: 1

MapStruct在使用Lombok时没有问题。

然而,如果您已经根据Lombok更改日志升级到Lombok 1.18.16,则需要添加

lombok-mapstruct-binding

在Gradle中:

annotationProcessor "org.projectlombok:lombok-mapstruct-binding:0.1.0"
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok-mapstruct-binding</artifactId>
    <version>0.1.0</version>
</dependency>
英文:

MapStruct works without issues with Lombok.

However, if you have upgraded to Lombok 1.18.16 as per the Lombok Changelog then you need to add

lombok-mapstruct-binding

In Gradle:

annotationProcessor &quot;org.projectlombok:lombok-mapstruct-binding:0.1.0&quot;
&lt;path&gt;
    &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
    &lt;artifactId&gt;lombok-mapstruct-binding&lt;/artifactId&gt;
    &lt;version&gt;0.1.0&lt;/version&gt;
&lt;/path&gt;

huangapple
  • 本文由 发表于 2020年10月17日 02:00:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64394188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定