JAXB 泛型 @XmlValue

发布于 2024-12-26 10:17:53 字数 769 浏览 4 评论 0原文

目标是使用 JAXB 生成以下 XML

<foo>
   <bar>string data</bar>
   <bar>binary data</bar>
</foo>

是否有解决方法允许通用 @XmlValue 字段(我需要存储byte[]String 数据)?以下是我想要的:

@XmlRootElement
public class Foo {
    private @XmlElement List<Bar> bars;
}

@XmlRootElement
public class Bar<T> {
    private @XmlValue T value;  // (*)
}

但我得到了这个例外

(*) IllegalAnnotationException:
@XmlAttribute/@XmlValue 需要引用映射到 XML 中文本的 Java 类型。

The goal is to produce the following XML with JAXB

<foo>
   <bar>string data</bar>
   <bar>binary data</bar>
</foo>

Is there a workaround to allow generic @XmlValue fields (I need to store byte[] and String data)? Below is what I desire:

@XmlRootElement
public class Foo {
    private @XmlElement List<Bar> bars;
}

@XmlRootElement
public class Bar<T> {
    private @XmlValue T value;  // (*)
}

But I get this exception

(*) IllegalAnnotationException:
@XmlAttribute/@XmlValue need to reference a Java type that maps to text in XML.

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

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

发布评论

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

评论(4

单调的奢华 2025-01-02 10:17:53

对于此用例,您可以利用 XmlAdapter 而不是 @XmlValue

BarAdapter

package forum8807296;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class BarAdapter extends XmlAdapter<Object, Bar<?>> {

    @Override
    public Bar<?> unmarshal(Object v) throws Exception {
        if(null == v) {
            return null;
        }
        Bar<Object> bar = new Bar<Object>();
        bar.setValue(v);
        return bar;
    }

    @Override
    public Object marshal(Bar<?> v) throws Exception {
        if(null == v) {
            return null;
        }
        return v.getValue();
    }

}

Foo

The XmlAdapter< /code> 使用 @XmlJavaTypeAdapter 注释与 bars 属性关联:

package forum8807296;

import java.util.List;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
public class Foo {
    private List<Bar> bars;

    @XmlElement(name="bar")
    @XmlJavaTypeAdapter(BarAdapter.class)
    public List<Bar> getBars() {
        return bars;
    }

    public void setBars(List<Bar> bars) {
        this.bars = bars;
    }

}

Bar

package forum8807296;

public class Bar<T> {
    private T value;

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }
}

Demo

您可以测试此示例使用以下演示代码:

package forum8807296;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Foo.class);

        Foo foo = new Foo();
        List<Bar> bars = new ArrayList<Bar>();
        foo.setBars(bars);

        Bar<String> stringBar = new Bar<String>();
        stringBar.setValue("string data");
        bars.add(stringBar);

        Bar<byte[]> binaryBar = new Bar<byte[]>();
        binaryBar.setValue("binary data".getBytes());
        bars.add(binaryBar);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(foo, System.out);
    }

}

输出

请注意输出如何包含 xsi:type 属性以保留值的类型。如果您这样做,您可以通过让 XmlAdapter 返回 String 而不是 Object 来消除 xsi:type 属性您需要自己处理从 String 到适当类型的转换,以进行解组操作:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foo>
    <bar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">string data</bars>
    <bar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:base64Binary">YmluYXJ5IGRhdGE=</bars>
</foo>

You could leverage an XmlAdapter for this use case instead of @XmlValue:

BarAdapter

package forum8807296;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class BarAdapter extends XmlAdapter<Object, Bar<?>> {

    @Override
    public Bar<?> unmarshal(Object v) throws Exception {
        if(null == v) {
            return null;
        }
        Bar<Object> bar = new Bar<Object>();
        bar.setValue(v);
        return bar;
    }

    @Override
    public Object marshal(Bar<?> v) throws Exception {
        if(null == v) {
            return null;
        }
        return v.getValue();
    }

}

Foo

The XmlAdapter is associated with the bars property using the @XmlJavaTypeAdapter annotation:

package forum8807296;

import java.util.List;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
public class Foo {
    private List<Bar> bars;

    @XmlElement(name="bar")
    @XmlJavaTypeAdapter(BarAdapter.class)
    public List<Bar> getBars() {
        return bars;
    }

    public void setBars(List<Bar> bars) {
        this.bars = bars;
    }

}

Bar

package forum8807296;

public class Bar<T> {
    private T value;

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }
}

Demo

You can test this example using the following demo code:

package forum8807296;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Foo.class);

        Foo foo = new Foo();
        List<Bar> bars = new ArrayList<Bar>();
        foo.setBars(bars);

        Bar<String> stringBar = new Bar<String>();
        stringBar.setValue("string data");
        bars.add(stringBar);

        Bar<byte[]> binaryBar = new Bar<byte[]>();
        binaryBar.setValue("binary data".getBytes());
        bars.add(binaryBar);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(foo, System.out);
    }

}

Output

Note how the output includes the xsi:type attributes to preserve the type of the value. You can eliminate the the xsi:type attribute by having your XmlAdapter return String instead of Object, if you do this you will need handle the conversion from String to the appropriate type yourself for the unmarshal operation:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foo>
    <bar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">string data</bars>
    <bar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:base64Binary">YmluYXJ5IGRhdGE=</bars>
</foo>
热情消退 2025-01-02 10:17:53

我无法让 @XmlValue 工作,因为一路上我总是遇到 NullPointerException — 不知道为什么。我想出了类似下面的东西。

完全删除您的 Bar 类,因为,当您希望它能够包含任何东西时,您可以简单地用 Object 表示它。

@XmlRootElement(name = "foo", namespace = "http://test.com")
@XmlType(name = "Foo", namespace = "http://test.com")
public class Foo {

  @XmlElement(name = "bar")
  public List<Object> bars = new ArrayList<>();

  public Foo() {}
}

如果不告诉 JAXB 您的类型正在使用哪些命名空间,则 foo 内的每个 bar 元素将包含单独的命名空间声明和内容 - package-info.java所有命名空间的内容仅用于幻想目的。

@XmlSchema(attributeFormDefault = XmlNsForm.QUALIFIED,
           elementFormDefault = XmlNsForm.QUALIFIED,
           namespace = "http://test.com",
           xmlns = {
               @XmlNs(namespaceURI = "http://test.com", prefix = ""),
               @XmlNs(namespaceURI = "http://www.w3.org/2001/XMLSchema-instance", prefix = "xsi"),
               @XmlNs(namespaceURI = "http://www.w3.org/2001/XMLSchema", prefix = "xs")})
package test;

import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

运行这个简单的测试会输出类似于 XML 片段的内容。

public static void main(String[] args) throws JAXBException {
  JAXBContext context = JAXBContext.newInstance(Foo.class);

  Foo foo = new Foo();
  foo.bars.add("a");
  foo.bars.add("b".getBytes());

  Marshaller marshaller = context.createMarshaller();
  marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
  marshaller.marshal(foo, System.out);
}

输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foo xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://test.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <bar xsi:type="xs:string">a</bar>
    <bar xsi:type="xs:base64Binary">Yg==</bar>
</foo>

I couldn't get @XmlValue working as I always got NullPointerException along the way—not sure why. I came up with something like the following instead.

Drop your Bar class entirely, because, as you want it to be able to contain anything you can simply represent it with Object.

@XmlRootElement(name = "foo", namespace = "http://test.com")
@XmlType(name = "Foo", namespace = "http://test.com")
public class Foo {

  @XmlElement(name = "bar")
  public List<Object> bars = new ArrayList<>();

  public Foo() {}
}

Without telling JAXB which namespaces your types are using every bar element inside a foo would contain separate namespace declarations and stuff—the package-info.java and all the namespace stuff serves only fancification purposes only.

@XmlSchema(attributeFormDefault = XmlNsForm.QUALIFIED,
           elementFormDefault = XmlNsForm.QUALIFIED,
           namespace = "http://test.com",
           xmlns = {
               @XmlNs(namespaceURI = "http://test.com", prefix = ""),
               @XmlNs(namespaceURI = "http://www.w3.org/2001/XMLSchema-instance", prefix = "xsi"),
               @XmlNs(namespaceURI = "http://www.w3.org/2001/XMLSchema", prefix = "xs")})
package test;

import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

Running this simple test would spout-out something similar to your XML snippet.

public static void main(String[] args) throws JAXBException {
  JAXBContext context = JAXBContext.newInstance(Foo.class);

  Foo foo = new Foo();
  foo.bars.add("a");
  foo.bars.add("b".getBytes());

  Marshaller marshaller = context.createMarshaller();
  marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
  marshaller.marshal(foo, System.out);
}

Output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foo xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://test.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <bar xsi:type="xs:string">a</bar>
    <bar xsi:type="xs:base64Binary">Yg==</bar>
</foo>
你的背包 2025-01-02 10:17:53

您是否有理由不简单地使用 byte[] 构造一个 String ?您真的需要通用吗?

Is there a reason you don't simply construct a String with your byte[]? Do you truly need a generic?

厌味 2025-01-02 10:17:53

我通常使用的技巧是创建具有所需类型的模式,然后使用 xjc 生成 Java 类并查看如何使用注释。 :) 我相信在 XML 模式中,byte[] 的正确类型映射是“base64Binary”,因此创建如下模式:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/NewXMLSchema" xmlns:tns="http://www.example.org/NewXMLSchema" elementFormDefault="qualified">
    <element name="aTest" type="base64Binary"></element>
</schema>

并运行 xjc 我们将生成以下代码:

@XmlElementDecl(namespace = "http://www.example.org/NewXMLSchema", name = "aTest")
public JAXBElement<byte[]> createATest(byte[] value) {
    return new JAXBElement<byte[]>(_ATest_QNAME, byte[].class, null, ((byte[]) value));
}

The trick I'm usually using is to create schema with types you want and then use xjc to generate Java classes and see how annotations are used. :) I believe in XML schema proper type mapping for byte[] is 'base64Binary', so creating schema like this:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/NewXMLSchema" xmlns:tns="http://www.example.org/NewXMLSchema" elementFormDefault="qualified">
    <element name="aTest" type="base64Binary"></element>
</schema>

and running xjc we would get following code generated:

@XmlElementDecl(namespace = "http://www.example.org/NewXMLSchema", name = "aTest")
public JAXBElement<byte[]> createATest(byte[] value) {
    return new JAXBElement<byte[]>(_ATest_QNAME, byte[].class, null, ((byte[]) value));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文