英文:
Boolean defaultValue MapStruct
问题
以下是翻译好的部分:
我正在尝试使用MapStruct为布尔字段设置defaultValue,但生成的代码完全忽略了它。
我的代码:
public class CreateEventRequest {
@NotNull
@JsonProperty
private Boolean isPrivate;
@JsonProperty
private Boolean friendCanInviteFriends;
@JsonProperty
private boolean showGuestList;
public boolean isPrivate() {
return isPrivate;
}
public String getDescription() {
return description;
}
public boolean isFriendCanInviteFriends() {
return friendCanInviteFriends;
}
public boolean isShowGuestList() {
return showGuestList;
}
}
我的映射器:
@Mapper(componentModel = "spring")
public interface CreateEventRequestMapper {
@Mapping(target = "showGuestList", source = "showGuestList", defaultExpression = "java(true)")
@Mapping(target = "friendCanInviteFriends", source = "friendCanInviteFriends", defaultValue = "true")
Event map(CreateEventRequest request);
}
生成的代码:
public class CreateEventRequestMapperImpl implements CreateEventRequestMapper {
@Override
public Event map(CreateEventRequest request) {
if ( request == null ) {
return null;
}
Event event = new Event();
event.setShowGuestList( request.isShowGuestList() );
event.setFriendCanInviteFriends( request.isFriendCanInviteFriends() );
event.setPrivate( request.isPrivate() );
return event;
}
}
正如您所看到的,我尝试使用基本/非基本类型,但它完全忽略了defaultValue。
我是否漏掉了什么?
谢谢!
英文:
I'm trying to set a defaultValue for a boolean field using MapStruct, but the generated code simply ignores it.
My code:
public class CreateEventRequest {
@NotNull
@JsonProperty
private Boolean isPrivate;
@JsonProperty
private Boolean friendCanInviteFriends;
@JsonProperty
private boolean showGuestList;
public boolean isPrivate() {
return isPrivate;
}
public String getDescription() {
return description;
}
public boolean isFriendCanInviteFriends() {
return friendCanInviteFriends;
}
public boolean isShowGuestList() {
return showGuestList;
}
}
My mapper:
@Mapper(componentModel = "spring")
public interface CreateEventRequestMapper {
@Mapping(target = "showGuestList", source = "showGuestList", defaultExpression = "java(true)")
@Mapping(target = "friendCanInviteFriends", source = "friendCanInviteFriends", defaultValue = "true")
Event map(CreateEventRequest request);
}
The generated code:
public class CreateEventRequestMapperImpl implements CreateEventRequestMapper {
@Override
public Event map(CreateEventRequest request) {
if ( request == null ) {
return null;
}
Event event = new Event();
event.setShowGuestList( request.isShowGuestList() );
event.setFriendCanInviteFriends( request.isFriendCanInviteFriends() );
event.setPrivate( request.isPrivate() );
return event;
}
}
As you can see, I've tried using primitive/non-primitive type but it simply ignores the defaultValue.
Am I missing something here?
Thanks!
答案1
得分: 3
问题在于源对象中的getter方法的返回类型始终是原始类型,无法为null,您需要返回Boolean
。
MapStruct不支持直接访问私有字段,这需要使用反射。
英文:
The problem is that the return type of your getter methods in the source object is always primitive which can't be null, you need to return Boolean
.
MapStruct doesn't support direct private field access which requires reflection.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论