【问题标题】:Using Jackson with generic types将 Jackson 与泛型类型一起使用
【发布时间】:2015-09-12 12:20:42
【问题描述】:

假设我有以下课程:

public class Sample<T> {
    private String smth;
}

我需要让“smth”属性的名称取决于泛型类型。例如:Sample&lt;String&gt; 应转为{"string":value}Sample&lt;Integer&gt; 应转为{"int":value}

我尝试使用 Jackson MixIn 但它只能应用于具体类(不是通用的)

任何人都可以提出一些解决办法吗?

【问题讨论】:

    标签: java json jackson mixins


    【解决方案1】:

    这个问题称为擦除

    如果你有这个代码

    Sample<String> mysample = ...
    

    并想在{"string":value}中转换mysample

    这是不可能的。

    在运行时, 信息不可用。这称为擦除。

    What is the concept of erasure in generics in Java?

    Effects of Type Erasure and Bridge Methods

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-07
      • 1970-01-01
      • 1970-01-01
      • 2010-09-08
      • 2011-07-24
      相关资源
      最近更新 更多