У меня есть один класс SyncRegistrationDto с 8 полями, и у меня есть подкласс SyncRegistrationFailureDto (суперкласс SyncRegistrationDto), который имеет только два поля. поэтому, возвращая объект подкласса, я также получаю значение переменной суперкласса как нуль в данных json.
Суперкласс:
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"
}
заранее спасибо.
Значит, вы хотите игнорировать пустые поля? И что вы используете для сериализации pojo в json? Swagger code-gen?
Да, Бриковски, у меня есть наследование, потому что мне нужно вернуть общий тип списка, который выглядит как List <SyncRegistrationDto>
РАЙАН ... Я не могу сказать, что хочу игнорировать пустые поля, я хочу игнорировать поля суперкласса только при создании объекта подкласса. Да, я использую swagger SyncRegistrationFailureDto syncRegistrationFailureDto = new SyncRegistrationFailureDto ();
Спасибо, manelseo @jsonIgnore выполняет мое требование.




Простым решением было бы использовать @JsonInclude(Include.NON_NULL).
Я думаю, что вопрос больше в том, почему у них есть отношения наследования, если многие поля / методы суперкласса ничего не значат для подкласса