JaxB编组通配符泛型扩展类

时间:2014-08-01 17:09:59

标签: java xml generics jaxb

我在编组java类时遇到了一个问题。

在代码段1中,A类使用List<B>,而代码段2使用List<B<? extends C>>

代码段1生成<?xml version="1.0" encoding="UTF-8" standalone="yes"?><a><test><item><name>Success!!</name></item></test></a>

而片段2缺少C类<?xml version="1.0" encoding="UTF-8" standalone="yes"?><a><test/></a>

内的数据

有人可以帮我解决这个问题吗?

摘录1:

public class JaxBXMLTest {
@Test
public void testMarshalling() throws JAXBException{
    testBaseClass();
}

private void testBaseClass()  throws JAXBException{
    A resp = new A();
    C test = new C();
    test.setName("Success!!");
    List<B> data = new ArrayList<B>();
    B wrap = new B();
    wrap.setItem(test);
    data.add(wrap);
    resp.setTest(data);

    JAXBContext context = JAXBContext.newInstance(A.class);
    Marshaller m = context.createMarshaller();
    //for pretty-print XML in JAXB

    // Write to System.out for debugging
     m.marshal(resp, System.out);
}

@XmlRootElement
@XmlType(name = "MyResponse")
public static class A{
    List<B> test;

    public List<B> getTest() {
        return test;
    }

    public void setTest(List<B> test) {
        this.test = test;
    }

}

public static class B{
    private C item;

    public C getItem() {
        return item;
    }

    public void setItem(C item) {
        this.item = item;
    }
}
public static class C{
    private String name;

    public String getName() {
        return name;
    }

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

}

摘录2:

public class JaxBXMLTestB {
@Test
public void testMarshalling() throws JAXBException {
    testSubClass();
}

private void testSubClass() throws JAXBException {
    A resp = new A();
    C test = new C();
    test.setName("Failure!!!");

    B<? extends C> wrap = new B<C>();
    wrap.setItem(test);

    List<B<? extends C>> finalObj = new ArrayList<B<? extends C>>();
    finalObj.add(wrap);
    resp.setTest(finalObj);

    JAXBContext context = JAXBContext.newInstance(A.class);
    Marshaller m = context.createMarshaller();
    // for pretty-print XML in JAXB
    // m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    // Write to System.out for debugging
    m.marshal(resp, System.out);
}

@XmlRootElement
@XmlType(name = "MySubClassResponse")
public static class A {
    List<B<? extends C>> test;

    public A(){
        test = new ArrayList<B<? extends C>>();
    }

    public List<B<? extends C>> getTest() {
        return test;
    }

    public void setTest(List<B<? extends C>> test) {
        this.test = test;
    }
}

public static class B<T extends C> {
    private T item;

    public T getItem() {
        return item;
    }

    public void setItem(C item) {
        this.item = (T) item;
    }
}

public static class C {
    private String name;

    public String getName() {
        return name;
    }

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

}

1 个答案:

答案 0 :(得分:1)

最后发现将@XmlAnyElement(lax=true)添加到字段是修复。