我有一堆从模式生成的类。我无法触及它们 - 对我来说是不可变的源泉。
这些类是架构中的顶级元素。每个都指向同名的类型:
<xsd:element name="Foo" type="FooType"/>
生成一个具有所有必要属性的 FooType
类,但它没有 @XmlRoot
注释。未生成类 Foo
。
在 JAXB 中,实现使用查找表并显式解码对象:
JAXBContext ctx ...
FooType x = ctx.createUnmarshaller().unmarshal(source, FooType.class);
在 spring-boot 中,使用 Jaxb2Marshaller,我无法给出类参数。看来,编码仅依赖 @XmlRoot
注释来发挥其魔力。
当设置 marshaller.setMappedClass(FooType.class)
时,springboot 解码器将使用它 - 但是,只有当我按需创建 Jaxb2Marshaller 时(看起来很昂贵),这才会在实例中设置一次,并且不是线程安全的。
我需要访问 JAXBContext 本身,以创建我自己的解码器。虽然这是可能的,但我没有在解码器上进行神奇的初始化。有用的方法 Jaxb2Marshaller#createUnmarshaller()
是 protected
。
我只看到将 Jaxb2Marshaller
扩展到我自己的虚拟对象以扩大对 protected 方法的访问的解决方案。这看起来不干净。
我宁愿完全放弃 Jaxb2Marshaller
并沿着旧应用程序的方式直接使用 JAXB。 Spring 的 JAXB 似乎并不打算在这里涵盖我的情况。
问题:我是否误解了这个概念? Jaxb2Marshaller.setMappedClass()
的预期用途是什么?
最佳答案
也许我没有完全解决您问题的核心,但如果缺少 @XmlRootElement 是您的问题,我建议您稍微调整您的架构。
我假设你有这样的东西:
<xsd:element name="Foo" type="FooType"/>
<!-- and somewhere in the schema also have -->
<xsd:complexType name="FooType">
<xsd:sequence>
<xsd:element name="blah" type="BlahType"/>
<xsd:element name="blah-blah" type="BlahBlahType" />
</xsd:sequence>
</xsd:complexType>
您需要做的是删除上面指定的两个并替换为:
<xsd:element name="Foo">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="blah" type="BlahType"/>
<xsd:element name="blah-blah" type="BlahBlahType" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
当您与 Foo 一起生成类时,它将自动具有 @RootXmlElement
关于java - Jaxb2Marshaller 无法以线程安全的方式解码非 XmlRoot 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48627704/