java—序列化列表生成的“item”节点

nukf8bse  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(340)

我有一个相当简单的java对象模型。预订课程有一个成员酒店列表(简化源代码)

class Booking {
  private Integer bookingId;
  private List<Hotel> hotels;
}

我想通过为每个类使用stdserailizer将预订序列化为xml。

public class HotelTransform extends StdSerializer<Hotel> {

    protected HotelTransform(Class<Hotel> t) {
        super(t);
    }

    private static final long serialVersionUID = 4102379614389258553L;

    @Override
    public void serialize(Hotel value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeStartObject();        
        gen.writeStringField("HName", value.getHotelName());
        gen.writeStringField("Start", value.m_startDateLocal.toString());
        gen.writeStringField("End", value.m_startDateLocal.toString());
        gen.writeEndObject();
    } 
}
public class BookingTransform extends StdSerializer<Booking> {

    protected BookingTransform(Class<Booking> t) {
        super(t);
    }

    @Override
    public void serialize(Booking value, JsonGenerator gen, SerializerProvider provider) throws IOException {

        gen.writeStartObject();        
        gen.writeObjectField("Entries", value.getBookBookingEntry());
        gen.writeEndObject();

    }
}

序列化本身不包含以下测试代码

public void simpleSmokeTest() {

        Booking booking = new Booking();
        booking.setBookingId(99);

        Hotel aHotel = new Hotel();
        aHotel.setHotelName("HEHotel");
        aHotel.setStartDateLocal(LocalDateTime.now());
        aHotel.setEndDateLocal(LocalDateTime.now());
        booking.addhotel(aHotel);

        Hotel aHotel2 = new Hotel();
        aHotel2.setHotelName("HEHotel2");
        aHotel2.setStartDateLocal(LocalDateTime.now());
        aHotel2.setEndDateLocal(LocalDateTime.now());
        booking.addhotel(aHotel2);

        ObjectMapper mapper = new XmlMapper();
        SimpleModule module = new SimpleModule();
        module.addSerializer(new BookingTransform(Booking.class));
        module.addSerializer(new HotelTransform(Hotel.class));
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        mapper.registerModule(module);

        try {
            String serialized = mapper.writeValueAsString(booking);
            m_logger.debug("Serialized to: " + serialized);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

生成的输出为:

<Booking>
  <Entries>
    <item>
      <HName>HEHotel</HName>
      <Start>2020-12-29T01:43:18.008480</Start>
      <End>2020-12-29T01:43:18.008480</End>
    </item>
    <item>
      <HName>HEHotel2</HName>
      <Start>2020-12-29T01:43:18.009463</Start>
      <End>2020-12-29T01:43:18.009463</End>
    </item>
  </Entries>
</Booking>

这已经接近我想要的,但我不希望我的酒店名单的条目命名为“项目”,我希望他们被命名为“酒店”。像这样:

<Booking>
  <Entries>
    <Hotel>
      <HName>HEHotel</HName>
      <Start>2020-12-29T01:43:18.008480</Start>
      <End>2020-12-29T01:43:18.008480</End>
    </Hotel>
    <Hotel>
      <HName>HEHotel2</HName>
      <Start>2020-12-29T01:43:18.009463</Start>
      <End>2020-12-29T01:43:18.009463</End>
    </Hotel>
  </Entries>
</Booking>

我真的尝试了很多事情没有。没有成功。有人能告诉我怎么解决这个问题吗?顺便说一下,在我的pojo中使用注解是没有选择的。
向哈里e。

zf2sa74q

zf2sa74q1#

我假设问题是因为json和xml处理数组的方式不同。
下面是来自 XMLSerializerProvider._startRootArray() :

protected void _startRootArray(ToXmlGenerator xgen, QName rootName) throws IOException
    {
        xgen.writeStartObject();
        // Could repeat root name, but what's the point? How to customize?
        xgen.writeFieldName("item");
    }

因此,对于xml,每个数组元素都被视为一个对象,默认字段名为item。
当前用例的一个选项是使用 Transformer 特定于xml,并在其中手动序列化 List 具体如下:

@Override
    public void serialize(Booking value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeStartObject();
        gen.writeFieldName("Entries");
        gen.writeStartObject();
        for (Hotel hotel : value.getBookBookingEntry()) {
            gen.writeFieldName("Hotel");
            gen.writeObject(hotel);
        }
        gen.writeEndObject();
        gen.writeEndObject();
    }

注意:如果您对json使用这个序列化程序,它将导致名为 Hotel 在对象内 Entries .

相关问题