简体   繁体   English

使用JAXB解组Java泛型对象

[英]Unmarshalling java generics objects using JAXB

I have something like this: 我有这样的事情:

@XmlRootElement(name = "attribute", namespace = "ns.attrib")
    public class Attribute<T> implements AuthEntity {

      private String name;
      private T value;

      @XmlAttribute
      @Override
      public String getName() {
         return name;
      }

      @Override
      public Optional<Set<AuthEntity>> getAuthEntities() {
        return Optional.empty();
      }

      @XmlAttribute
      public T getValue() {
        return value;
      }

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

      public void setName(String name) {
        this.name = name;
      }

      @Override
      public int hashCode() {
        return Objects.hash(getName(), getValue());
      }

      @Override
      public boolean equals(Object obj) {
        if (this == obj)
          return true;
        if (obj == null)
          return false;
        if (getClass() != obj.getClass())
          return false;
        @SuppressWarnings("rawtypes")
        GSAttribute other = (GSAttribute) obj;
        if (!name.equals(other.name))
          return false;
        if (value == null) {
          if (other.value != null)
            return false;
        } else if (!value.equals(other.value))
          return false;
        return true;
      }
}

I have to do some CRUD on an xml based persistence layer using some custom interfaces so I have to marshall/unmarshall the above type using JAXB (ver. 2.2.11). 我必须使用一些自定义接口在基于xml的持久层上进行一些CRUD,所以我必须使用JAXB(版本2.2.11)来编组/解编上述类型。 Ok, let's start with some test to be confident about that. 好的,让我们开始进行一些测试来对此充满信心。 I wrote few: 我写了几篇:

@Test
public void deserializeEmptyAttribute() throws JAXBException {
    String xml = "<attribute />";
    Attribute expected = new Attribute<>();
    Attribute actual = JAXB.unmarshal(new StringReader(xml), Attribute.class);
    Assert.assertEquals(expected, actual);
}

@Test
public void deserializeIntegerAttribute() throws Exception {
    String xml = "<attribute name=\"somename\" value=\"105\"/>";
    Attribute<Integer> expected = new Attribute<>();
    expected.setName("somename");
    expected.setValue(105);
    Attribute actual = JAXB.unmarshal(new StringReader(xml), Attribute.class);
    Assert.assertEquals(expected, actual);
}

@Test
public void deserializeStringAttribute() {
    String xml = "<attribute name=\"somename\" value=\"somevalue\"/>";
    Attribute<String> expected = new Attribute<>();
    expected.setName("somename");
    expected.setValue("somevalue");
    Attribute actual = JAXB.unmarshal(new StringReader(xml), Attribute.class);
    Assert.assertEquals(expected, actual);
}

Each test fail with this stack: 每个测试在此堆栈上均失败:

java.lang.NullPointerException
 at com.sun.xml.bind.v2.runtime.reflect.TransducedAccessor.get(TransducedAccessor.java:167)
 at com.sun.xml.bind.v2.runtime.property.AttributeProperty.<init>(AttributeProperty.java:91)
 at com.sun.xml.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:108)
 at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:181)
 at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:503)
 at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:320)
 at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:139)
 at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1138)
 at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:162)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:247)
 at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:234)
 at javax.xml.bind.ContextFinder.find(ContextFinder.java:441)
 at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:641)
 at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
 at javax.xml.bind.JAXB$Cache.<init>(JAXB.java:112)
 at javax.xml.bind.JAXB.getContext(JAXB.java:139)
 at javax.xml.bind.JAXB.unmarshal(JAXB.java:242)
...

I am pretty new to JAXB (forgot the most years ago) and I'm having some trouble achieving to run tests ok. 我对JAXB还是很陌生(大多数年前都被遗忘了),但在运行正常的测试时遇到了一些麻烦。 What am I missing? 我想念什么? I see there are a lot of post/answers similar but they didn't help (or I didn't get them). 我看到有很多类似的帖子/答案,但是它们没有帮助(或者我没有得到)。

You are getting this error because, you are using @XmlAttribute on a type which is not a simple schema type. 您收到此错误的原因是,您在非简单模式类型的类型上使用@XmlAttribute If you change your value to @XmlElement then it should work. 如果将value更改为@XmlElement则它应该可以工作。

If the type of the field or the property is a non collection type, then the type of the property or field must map to a simple schema type 如果字段或属性的类型是非集合类型,则属性或字段的类型必须映射为简单的架构类型

https://docs.oracle.com/javase/8/docs/api/javax/xml/bind/annotation/XmlAttribute.html https://docs.oracle.com/javase/8/docs/api/javax/xml/bind/annotation/XmlAttribute.html

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

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