且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将Jackson ObjectMapper与泛型一起使用到POJO而不是LinkedHashMap

更新时间:2023-01-16 23:38:55

首先,它适用于Longs因为那是排序本机类型,以及JSON整数的默认绑定。

First of all, it works with Longs because that is sort of native type, and as such default binding for JSON integral numbers.

但是为什么泛型类型信息没有正确传递:这很可能是由于方式JAX-RS API将类型传递给 MessageBodyReader s和 MessageBodyWriter s - 传递 java。 lang.reflect.Type 不足以(不幸!)足以传递实际的通用声明(有关此内容的更多信息,请阅读此博客条目)。

But as to why generic type information is not properly passed: this is most likely due to problems with the way JAX-RS API passes type to MessageBodyReaders and MessageBodyWriters -- passing java.lang.reflect.Type is not (unfortunately!) enough to pass actual generic declarations (for more info on this, read this blog entry).

一个简单的解决方法是创建辅助类型,如:

One easy work-around is to create helper types like:

class MyTypeIdentifierArray extends JsonArray<TypeIdentifier> { }

并使用该类型 - 事物将正常工作,因为超类型通用信息始终保留。

and use that type -- things will "just work", since super-type generic information is always retained.