布尔默认值 MapStruct

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

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.

huangapple
  • 本文由 发表于 2020年5月30日 06:06:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/62095284.html
匿名

发表评论

匿名网友

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

确定