简体   繁体   English

如何使用FasterXML库序列化POJO列表

[英]How to serialize a list of POJO using FasterXML library

I'm using FasterXML to serialize POJO. 我正在使用FasterXML序列化POJO。 I want to serialize a list of my POJO. 我想序列化我的POJO列表。 When serialize a signle POJO I get the expected xml (there's one problem --> question 2) Here's my code: 序列化一个信号POJO时,我得到了预期的xml(有一个问题->问题2),这是我的代码:

List<Movie> movies = new ArrayList<>();
// add movies
JacksonXmlModule module = new JacksonXmlModule();
module.setDefaultUseWrapper(false);
xmlMapper = new XmlMapper(module);
xmlMapper.disable(MapperFeature.AUTO_DETECT_CREATORS,
            MapperFeature.AUTO_DETECT_FIELDS,
            MapperFeature.AUTO_DETECT_GETTERS,
            MapperFeature.AUTO_DETECT_IS_GETTERS,
            MapperFeature.AUTO_DETECT_SETTERS,
            MapperFeature.USE_GETTERS_AS_SETTERS);
String xml = xmlMapper.writeValueAsString(movies);

I get this: 我得到这个:

<ArrayList>
    <item imdbID="tt0077687" title="The Hobbit" year="1977"/>
</ArrayList>

Here's what I want: 这就是我想要的:

<movies>
    <movie imdbID="tt0077687" title="The Hobbit" year="1977"/>
    <movie imdbID="tt0077687" title="title2" year="1977"/>
</movies>

or 要么

<movie imdbID="tt0077687" title="The Hobbit" year="1977"/>
<movie imdbID="tt0077687" title="title2" year="1977"/>
  1. When I serialize a movie I get this: 当我序列化电影时,我得到以下信息:

Is it possible to get this: 是否有可能得到这个:

<movie imdbID="tt0077687" title="The Hobbit" year="1977"><movie>

As a general rule, do not try serializing List , Map s or arrays directly as the root-level value: always use a Bean (POJO). 通常,请勿尝试直接序列化ListMap或数组作为根级值:始终使用Bean(POJO)。 Properties may be of any types, recursively. 属性可以递归地是任何类型。

The problem is that Java type erasure make things problematic for collection and Map types in general (even with JSON); 问题在于,Java类型擦除通常会导致集合和Map类型出现问题(即使使用JSON); but there are additional problems for XML. 但是XML还有其他问题。

So while it may seem unnecessary, I have found it safest to have a simple Object as the root value, even if it's only something like: 因此,尽管看起来似乎没有必要,但我发现以简单的Object作为根值是最安全的,即使它只是诸如此类:

public class Response {
   public List<Movie> movies;
}

Having said that, to change the name of root element can be done multiple ways. 话虽如此,更改根元素的名称可以通过多种方式完成。 One possibility is to use Jackson annotation @JsonRootName (despite "Json" in there, it applies to all formats). 一种可能性是使用Jackson批注@JsonRootName (尽管其中有“ Json”,它适用于所有格式)。

Or, you can use ObjectWriter , override root name with: 或者,您可以使用ObjectWriter ,使用以下ObjectWriter覆盖根名称:

String xml mapper.writer().withRootName("movies").writeValueAsString(movies);

You'll probably have to annotate your movies variable. 您可能需要注释movies变量。 (And likely either pull it out into a field or declare an object to encapsulate it.) Check the annotations on this page to see if something matches. (并且可能将其拉出到字段中或声明一个对象来封装它。)检查此页上的注释以查看是否有匹配项。 Sorry I can't be more specific. 抱歉,我不能更具体。

https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM