【问题标题】:Rename Element Name in Generic Classes [duplicate]重命名泛型类中的元素名称[重复]
【发布时间】:2016-12-19 19:22:33
【问题描述】:

我正在使用 Jackson,我正在使用通用类来响应如下:

public class GenericListInfoResponse<T> extends GenericResponse {

    private T resultInfo;

    public GenericListInfoResponse(T resultInfo) {
        super();
        this.resultInfo = resultInfo;
    }

    public T getResultInfo() {
        return resultInfo;
    }

    public void setResultInfo(T resultInfo) {
        this.resultInfo = resultInfo;
    }

}

我设置了一些数据来自我的服务:

TitleListInfo titleListInfo = dropdownDataService.getTitleList(transactionId);
response.setResultInfo(titleListInfo);

之后就是返回这种JSON:

{
  "resultInfo": {
    "combinedMessage": "",
    "messageCode": null
    ...
}

我想重命名元素名称而不是“resultInfo”。例如“titleInfo”

【问题讨论】:

    标签: java json jackson


    【解决方案1】:

    我猜您想这样做而不只是更改变量的名称?尝试使用 @JsonProperty("titleInfo") 注释字段,它应该可以工作。

    @JsonProperty("titleInfo")
    private T resultInfo;
    

    如果不起作用,请尝试对 getter 和 setter 进行注释。

    【讨论】:

    • 感谢您的回答,但我需要动态的 @JsonProperty 名称。我将重用我的这个类另一个包。我想重命名它,例如 userInfo,另一个地方可能是 meberInfo ...
    • 哦,抱歉,我读到这篇文章后,这似乎是你想要的。那就看看吧:stackoverflow.com/questions/22321155/…
    • @Whatsz 谢谢它是正确的。我将我的问题标记为重复。
    猜你喜欢
    • 2014-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    • 2018-12-11
    • 1970-01-01
    • 2017-06-29
    相关资源
    最近更新 更多