JAXB 编组集<对象>

发布于 2024-11-14 21:42:57 字数 1121 浏览 5 评论 0原文

我有一个与此类似的对象:

public class Obj {
    @XmlElement(name="value")
    public Set<Object> values;
}

当编组时,这会生成一个 xml,如下所示:

<Obj>
    <value xsi:type="xs:dateTime" xmlns:xs="http://www.w3.org/2001/XMLSchema">2009-02-14T00:31:30.001+01:00</value>
    <value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">test</value>
</Obj>

但是,我想更改其中一些值(例如用于序列化 DateTimestamp< 的日期格式/code> 对象),并摆脱烦人的 xsi 属性(但这并不是真正的要求,我可以忍受)

我尝试添加一个 @XmlJavaTypeAdapter 到values,但在适配器中我得到了完整的Set来适应,而不是单个元素。我也尝试过使用包适配器,但是,由于我的 Set 用于 Object,我无法放置 @XmlJavaTypeAdapter(type) 属性。

另外,我尝试使用 @XmlJavaTypeAdapter(value=MyAdapter.class, type=Timestamp.class) 来仅获取我想要的对象内的值的适配器。

所以问题是,有人知道如何让适配器为此工作吗?或者,也许每次序列化 DateTimestamp 对象时都更改日期格式?

提前致谢!

I have an object similar to this:

public class Obj {
    @XmlElement(name="value")
    public Set<Object> values;
}

When marshaling, this is generating an xml like:

<Obj>
    <value xsi:type="xs:dateTime" xmlns:xs="http://www.w3.org/2001/XMLSchema">2009-02-14T00:31:30.001+01:00</value>
    <value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">test</value>
</Obj>

However, I want to change some of that values (like the date format used for serializing Date and Timestamp objects), and also get rid of the annoying xsi attributes (but this is not really a requirement, I can live with that)

I've tried adding a @XmlJavaTypeAdapter to values, but in the adapter I get the full Set<Object> to adapt, instead of single elements. I've also tried with a package adapter, but, as my Set is for Object, I cannot put the @XmlJavaTypeAdapter(type) attribute.

Also, I've tried with @XmlJavaTypeAdapter(value=MyAdapter.class, type=Timestamp.class) to get only an adapter for the values inside that Object that I want.

So the question is, does someone know a way to get an adapter to work for this? Or maybe, change the date format every time a Date or Timestamp object is serialized?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

标点 2024-11-21 21:42:57

必须在包级别指定带有 type 属性的 @XmlJavaTypeAdapter。当以这种方式使用时,它指示指定包中该类型的所有用法都使用 XmlAdapter 进行转换。例如,如果您有一个类似的 package-info.java

@XmlJavaTypeAdapters({
    @XmlJavaTypeAdaptor(type=Timestamp.class, value=MyAdapter.class)
})
package org.example;

那么该包中的类带有 Timestamp 字段。

package org.example;
public class Obj {
    public Timestamp aTimestamp;
}

指定的适配器将用于转换时间戳。我怀疑这适用于您的 Set案例,但我自己还没有尝试过。

xsi:type 属性的原因是 JAX-B 喜欢生成可以反序列化的 XML,因此它需要指示它是什么类型,否则它只能将所有内容解析为字符串。您可以使用 @XmlElementRef 注释来创建架构替换组,但在这种情况下,将使用不同的元素名称生成 XML。例如

public class Obj {
    @XmlElementRefs({
        @XmlElementRef(type=String.class, name="string"),
        @XmlElementRef(type=Timestamp.class, name="timestamp")
    })
    public Set<Object> value;
}

,如果集合中有时间戳和字符串,将生成以下 XML 结构。在这种情况下,xsi:type 属性是不必要的,因为 JAX-B 可以根据元素名称判断要创建什么类型。

<Obj>
    <timestamp>2009-02-14T00:31:30.001+01:00</timestamp>
    <string>test</string>
</Obj>

我强烈建议使用 @XmlElementWrapper<如果您打算采用这种方法,则使用 /a> 注释来包装所有设置的项目。

如果您想要的只是一组简单的字符串,并且不关心反序列化回具有正确类型的 Java(或任何其他)对象,那么最简单的解决方案是使用一个 XmlAdapter,它只适应完整的 < code>Set转换为 Set 并自行处理转换。

@XmlJavaTypeAdapter with the type property has to be specified on the package level. When used in this way it indicates that all usages of that type within the specified package are converted using the XmlAdapter. E.g. if you have a package-info.java like

@XmlJavaTypeAdapters({
    @XmlJavaTypeAdaptor(type=Timestamp.class, value=MyAdapter.class)
})
package org.example;

Then a class in that package with a Timestamp field.

package org.example;
public class Obj {
    public Timestamp aTimestamp;
}

The specified adapter will be used to convert the timestamp. I suspect that this will work for your Set<Object> case but I haven't tried it myself.

The reason for the xsi:type attribute is that JAX-B likes to produce XML it can deserialize, so it needs to indicate what type it is or it could only parse everything back as strings. You can get rid of this attribute by using the @XmlElementRef annotation to create a schema substitution group, but in this case the XML will be produced with different element names. E.g.

public class Obj {
    @XmlElementRefs({
        @XmlElementRef(type=String.class, name="string"),
        @XmlElementRef(type=Timestamp.class, name="timestamp")
    })
    public Set<Object> value;
}

Would produce the following XML structure if you had a timestamp and a string in the set. In this scenario the xsi:type attribute is unnecessary since JAX-B can tell what type to create from the element name.

<Obj>
    <timestamp>2009-02-14T00:31:30.001+01:00</timestamp>
    <string>test</string>
</Obj>

I would strongly recommend using the @XmlElementWrapper annotation to wrap up all the set items if you're going to take this approach.

If all you're after is a simple set of strings that you don't care about deserializing back to Java (or any other) objects with the correct types, then the simplest solution is to have an XmlAdapter that does just adapt the full Set<Object> into a Set<String> and handle the conversion yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文