英文:
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 "org.projectlombok:lombok-mapstruct-binding:0.1.0"
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.1.0</version>
</path>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论