java - JaxB 编码通配符泛型扩展类

标签 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;
    }
}

最佳答案

最后发现在字段中添加 @XmlAnyElement(lax=true) 是解决方法。

关于java - JaxB 编码通配符泛型扩展类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25085353/

相关文章:

java - 如何区分 Spring Rest Controller 中部分更新的 null 值和未提供的值

java - 在 JDO/DataNucleus 中,我可以查询 Joda-Time DateTime 对象吗?

xml - Asyncio 返回 future 等待结果

java - 从 JNI 声明函数

php - 如何使用 php 导出 mysql 表

xml - 使用 XSLT 更改 SOAP namespace

swift - 使用它的通用 ObjectType 的 PHFetchResult 扩展?

java - 在也实现相同类的基类中使用泛型的好处

ios - Swift 泛型类如何从中创建类型对象

hibernate 的Java通用问题,如何处理T get(K id)