如何让超类的继承属性出现在子类的对象中?

How to have the inherited properties of superclass appear in an object in the subclass?

我有一个 DTO,它使用消息 属性 以及 getter 和 setter 扩展抽象基础 DTO。目前,当我 运行 我的服务时,我得到这个 JSON 对象作为输出。

{
    "message": null,
    "id_key": "014",
    "status_flag": null,
    "modified_by": "Dev",
    "modified_date": "2018-05-30"
}

正在继承消息字段。有没有办法像这样将继承的字段设置在它自己的子对象中。

{
    "_errors": {
        "message": null
    },
    "id_key": "014",
    "status_flag": null,
    "modified_by": "Dev",
    "modified_date": "2018-05-30"
}

我想避免在继承 class 中创建 BaseDTO 对象以及 getter 和 setter。我正在尝试重构很多 DTO,并且可以进行全部替换以将 extends BaseDTO 添加到它们中的每一个。谢谢!

创建 "Errors" class:

public class Errors {

    @JsonProperty("message")
    public String message;

    ...
}

并在你的 BaseDTO 中使用它:

@JsonProperty("_errors")
public Errors errors;

因此,每个继承 BaseDTO 的 class 都会有“_errors”属性,里面有 "message" 属性。