【问题标题】:nested @XmlJavaTypeAdapter annotations ignored where nested objects are generic when using eclispelink嵌套的 @XmlJavaTypeAdapter 注释在使用 eclispelink 时嵌套对象是通用的地方被忽略
【发布时间】:2011-09-15 19:14:56
【问题描述】:

在编组对象树时,我正在使用@XmlJavaTypeAdapter。一些适配器返回本身具有@XmlJavaTypeAdapter 注释的类的对象。当我使用与 websphere 7 一起打包的 JAXB 实现时,这工作得很好,但是当我使用 org.eclipse.persistence.jaxb.JAXBContextFactory 时,第一个适配器返回的对象上的 @XmlJavaTypeAdapter 注释将被忽略。这是一个已知问题,还是我做错了什么?

public class Demo {

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

        System.out.println(jc.getClass());

        Root root = new Root();
        A a = new A();
        root.a = a;

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

}

@XmlRootElement
public class Root {

   @XmlJavaTypeAdapter(AAdapter.class)
   public A a;

}

一个

public class A {

   public B b = new B();

}

B

public class B {

  @XmlJavaTypeAdapter(GAdapter.class)
  @XmlElement(name="b")
  public G<C> gc = new G<C>();

  public B(){
    gc.t = new C();
  }
}

C

public class C {

    public String c = "Foo";

}

G

public class G<T> {

  T t;

}

然后是A的适配器...

public class AAdapter extends XmlAdapter<B, A> {

    @Override
    public A unmarshal(B b) throws Exception {

        A a = new A();
        a.b = b;

        return a;
    }

    @Override
    public B marshal(A a) throws Exception {

        return a.b;
    }

}

以及泛型类型的适配器

public class GAdapter<T> extends XmlAdapter<T, G<T>> {

  @Override
  public G<T> unmarshal(T c) throws Exception {

    return new G<T>();
  }

  @Override
  public T marshal(G<T> g) throws Exception {

    return g.t;
  }

}

当与 com.sun.xml.bind.v2.runtime.JAXBContextImpl 类编组时

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<root>

    <a>

    <b xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="c">

        <c>Foo</c>

    </b>

    </a>

</root>

当与org.eclipse.persistence.jaxb.JAXBContext 编组时

<?xml version="1.0" encoding="UTF-8"?>

<root>

   <a>

      <b>forum237.C@23752375</b>

   </a>

</root>

我认为问题出在泛型类型上。目标是跳过对泛型类型进行封送处理,仅封送T,以及处理T 的注释(如果有)。

【问题讨论】:

    标签: generics annotations jaxb eclipselink moxy


    【解决方案1】:

    注意:我是EclipseLink JAXB (MOXy) 领导。

    我已经能够重现此问题并确认这是一个错误 (https://bugs.eclipse.org/350626)。

    package example;
    
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    
    @XmlRootElement
    public class Root {
    
        @XmlJavaTypeAdapter(AAdapter.class)
        public A<B> a;
    
    }
    

    一个

    package example;
    
    public class A<T> {
    
        public T t;
    
    }
    

    适配器

    package example;
    
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class AAdapter<T> extends XmlAdapter<T, A<T>> {
    
        @Override
        public A<T> unmarshal(T v) throws Exception {
            return new A<T>();
        }
    
        @Override
        public T marshal(A<T> v) throws Exception {
            return v.t;
        }
    
    }
    

    B

    package example;
    
    public class B {
    
    }
    

    演示

    package example;
    
    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(Root.class, B.class);
    
            Root root = new Root();
            A<B> a = new A<B>();
            a.t = new B();
            root.a = a;
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(root, System.out);
        }
    
    }
    

    输出

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
       <a>example.B@3ecfff</a>
    </root>
    

    预期输出

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
        <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="b"/>
    </root>
    

    【讨论】:

      猜你喜欢
      • 2021-01-31
      • 2014-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-04
      • 2021-05-27
      相关资源
      最近更新 更多