在创建JSON期间在子类中隐藏值

时间:2018-11-28 07:39:13

标签: java json rest

我有一个具有8个字段的SyncRegistrationDto类,并且我有一个仅具有两个字段的子类SyncRegistrationFailureDto(超类SyncRegistrationDto)。因此,在返回子类的对象时,我也在json数据中获取超类变量值作为null。

特级:

public class SyncRegistrationDto implements Serializable {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = -3922338139042373367L;

    /** The registration id. */
    private String registrationId;

    /** The sync type dto. */
    private SyncTypeDto syncType;

    /** The parent registration id. */
    private String parentRegistrationId;

    /** The sync status dto. */
    private SyncStatusDto syncStatus;

    /** The status comment. */
    private String statusComment;

    /** The lang code. */
    private String langCode;

    /** The is active. */
    @ApiModelProperty(hidden = true)
    private Boolean isActive;

    /** The is deleted. */
    @ApiModelProperty(hidden = true)
    private Boolean isDeleted;

    /**
     * Instantiates a new sync registration dto.
     */
    public SyncRegistrationDto() {
        super();
    }

    /**
     * Instantiates a new sync registration dto.
     *
     * @param registrationId
     *            the registration id
     * @param syncTypeDto
     *            the sync type dto
     * @param parentRegistrationId
     *            the parent registration id
     * @param syncStatusDto
     *            the sync status dto
     * @param statusComment
     *            the status comment
     * @param langCode
     *            the lang code
     */
    public SyncRegistrationDto(String registrationId, SyncTypeDto syncTypeDto, String parentRegistrationId,
            SyncStatusDto syncStatusDto, String statusComment, String langCode) {
        super();
        this.registrationId = registrationId;
        this.syncType = syncTypeDto;
        this.parentRegistrationId = parentRegistrationId;
        this.syncStatus = syncStatusDto;
        this.statusComment = statusComment;
        this.langCode = langCode;
    }

    /**
     * Gets the registration id.
     *
     * @return the registration id
     */
    public String getRegistrationId() {
        return registrationId;
    }

    /**
     * Sets the registration id.
     *
     * @param registrationId
     *            the new registration id
     */
    public void setRegistrationId(String registrationId) {
        this.registrationId = registrationId;
    }

    /**
     * Gets the parent registration id.
     *
     * @return the parent registration id
     */
    public String getParentRegistrationId() {
        return parentRegistrationId;
    }

    /**
     * Sets the parent registration id.
     *
     * @param parentRegistrationId
     *            the new parent registration id
     */
    public void setParentRegistrationId(String parentRegistrationId) {
        this.parentRegistrationId = parentRegistrationId;
    }

    /**
     * Gets the status comment.
     *
     * @return the status comment
     */
    public String getStatusComment() {
        return statusComment;
    }

    /**
     * Sets the status comment.
     *
     * @param statusComment
     *            the new status comment
     */
    public void setStatusComment(String statusComment) {
        this.statusComment = statusComment;
    }

    /**
     * Gets the lang code.
     *
     * @return the lang code
     */
    public String getLangCode() {
        return langCode;
    }

    /**
     * Sets the lang code.
     *
     * @param langCode
     *            the new lang code
     */
    public void setLangCode(String langCode) {
        this.langCode = langCode;
    }

    /**
     * Gets the checks if is active.
     *
     * @return the checks if is active
     */
    public Boolean getIsActive() {
        return isActive;
    }

    /**
     * Sets the checks if is active.
     *
     * @param isActive
     *            the new checks if is active
     */
    public void setIsActive(Boolean isActive) {
        this.isActive = isActive;
    }

    /**
     * Gets the sync type dto.
     *
     * @return the sync type dto
     */
    public SyncTypeDto getSyncType() {
        return syncType;
    }

    /**
     * Sets the sync type dto.
     *
     * @param syncTypeDto
     *            the new sync type dto
     */
    public void setSyncType(SyncTypeDto syncTypeDto) {
        this.syncType = syncTypeDto;
    }

    /**
     * Gets the sync status dto.
     *
     * @return the sync status dto
     */
    public SyncStatusDto getSyncStatus() {
        return syncStatus;
    }

    /**
     * Sets the sync status dto.
     *
     * @param syncStatusDto
     *            the new sync status dto
     */
    public void setSyncStatus(SyncStatusDto syncStatusDto) {
        this.syncStatus = syncStatusDto;
    }

    /**
     * Gets the checks if is deleted.
     *
     * @return the checks if is deleted
     */
    public Boolean getIsDeleted() {
        return isDeleted;
    }

    /**
     * Sets the checks if is deleted.
     *
     * @param isDeleted
     *            the new checks if is deleted
     */
    public void setIsDeleted(Boolean isDeleted) {
        this.isDeleted = isDeleted;
    }

子类:

public class SyncRegistrationFailureDto extends SyncRegistrationDto implements Serializable {


    private static final long serialVersionUID = 4456270091048678274L;

    private String registrationId;


    private String messgae;

    @Override
    public String getRegistrationId() {
        return registrationId;
    }
    @Override
    public void setRegistrationId(String registrationId) {
        this.registrationId = registrationId;
    }


    public String getMessgae() {
        return messgae;
    }

    public void setMessgae(String messgae) {
        this.messgae = messgae;
    }

    public SyncRegistrationFailureDto(String registrationId,String messgae) {
        this.registrationId = registrationId;
        this.messgae = messgae;
    }


    public SyncRegistrationFailureDto() {
        super();
    }



}

所以在返回子类对象的同时,我也得到了超类的变量,但是我只想在子类中有两个可用的值,我该如何实现呢?

 {
    "registrationId": "12345",
    "syncType": null,
    "parentRegistrationId": null,
    "syncStatus": null,
    "statusComment": null,
    "langCode": null,
    "isActive": null,
    "isDeleted": null,
    "messgae": "Not active or not valid"
  }

expected o/p:

{
    "registrationId": "12345",
    "messgae": "Not active or not valid"
  }

预先感谢。

1 个答案:

答案 0 :(得分:0)

简单的解决方案是使用@JsonInclude(Include.NON_NULL)