Spring/json:转换类型化集合,如 List<MyPojo> [英] Spring/json: Convert a typed collection like List<MyPojo>
问题描述
我正在尝试通过 Spring Rest 模板编组一个列表:List
对象.
I'm trying to marshal a list: List<Pojo>
objects via the Spring Rest Template.
我可以传递简单的 Pojo
对象,但我找不到任何描述如何发送 List
对象的文档.
I can pass along simple Pojo
objects, but I can't find any documentation that describes how to send a List<Pojo>
objects.
Spring 使用 Jackson JSON 来实现 HttpMessageConverter
.杰克逊文档涵盖了这一点:
Spring is using Jackson JSON to implement the HttpMessageConverter
. The jackson documentation covers this:
除了绑定到 POJO 和简单"类型,有一种附加变体:绑定到通用(类型化)容器.这个案例由于需要特殊处理所谓的类型擦除(Java 使用在某种程度上实现泛型向后兼容的方式),其中阻止你使用类似的东西Collection
(不编译).
In addition to binding to POJOs and "simple" types, there is one additional variant: that of binding to generic (typed) containers. This case requires special handling due to so-called Type Erasure (used by Java to implement generics in somewhat backwards compatible way), which prevents you from using something like
Collection<String>.class
(which does not compile).
所以如果你想把数据绑定到一个Map
你需要使用:
So if you want to bind data into a
Map<String,User>
you will need to use:
Map
其中 TypeReference
只需要传递泛型类型定义(通过在这种情况下是任意内部类):重要的部分是 定义类型绑定到.
where TypeReference
is only needed to
pass generic type definition (via
anynomous inner class in this case):
the important part is
<Map<String,User>>
which defines type
to bind to.
这可以在 Spring 模板中完成吗?我看了一眼代码,这让我不以为然,但也许我只是不知道一些技巧.
Can this be accomplished in the Spring template? I took a glance at the code and it makes me thing not, but maybe I just don't know some trick.
解决方案
最终的解决方案,感谢以下有用的答案,不是发送列表,而是发送一个简单扩展列表的对象,例如:class PojoList extends ArrayList
.Spring 可以成功地封送这个对象,它完成与发送 List
相同的事情,尽管它的解决方案不那么干净.我还在春季发布了一个 JIRA,让他们在 HttpMessageConverter
接口中解决这个缺点.
The ultimate solution, thanks to the helpful answers below, was to not send a List, but rather send a single object which simply extends a List, such as: class PojoList extends ArrayList<Pojo>
. Spring can successfully marshal this Object, and it accomplishes the same thing as sending a List<Pojo>
, though it be a little less clean of a solution. I also posted a JIRA in spring for them to address this shortcoming in their HttpMessageConverter
interface.
推荐答案
如果我阅读了 docs for MappingJacksonHttpMessageConverter
对,您必须创建并注册 MappingJacksonHttpMessageConverter 的子类code> 并覆盖
getJavaType(Class>)
方法:
If I read the docs for MappingJacksonHttpMessageConverter
right, you will have to create and register a subclass of MappingJacksonHttpMessageConverter
and override the getJavaType(Class<?>)
method:
返回 Jackson JavaType具体类.默认实现回报TypeFactory.type(java.lang.reflect.Type),但这可以在子类,允许自定义通用集合处理.为了实例:
Returns the Jackson JavaType for the specific class. Default implementation returns TypeFactory.type(java.lang.reflect.Type), but this can be overridden in subclasses, to allow for custom generic collection handling. For instance:
protected JavaType getJavaType(Class<?> clazz) {
if (List.class.isAssignableFrom(clazz)) {
return TypeFactory.collectionType(ArrayList.class, MyBean.class);
} else {
return super.getJavaType(clazz);
}
}
这篇关于Spring/json:转换类型化集合,如 List<MyPojo>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!