2010-08-26 2 views
3

есть типы:JAXB маршаллер анг дженерики (2)

class A{} 

@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER) 
@XmlType(propOrder = {"obj"}) 
@XmlRootElement(name = "response") 
public class B<T extends A> extends A{ 
    private T obj; 

    @XmlElement(required = true) 
    public T getObj() { 
    return obj; 
    } 
} 

Когда я пытаюсь выстроить это я получаю сообщение об ошибке:

org.springframework.oxm.MarshallingFailureException: JAXB marshalling exception; nested exception is javax.xml.bind.MarshalException 
- with linked exception: 
[com.sun.istack.internal.SAXException2: unable to marshal type "com.my.B" as an element because it is missing an @XmlRootElement annotation] 

ли jaxbMarshaller работать с родовым? Любые идеи?

благодаря

ответ

1

Как становится создан ваш JAXBContext? Вам нужно будет убедиться, что он знает о B.class. Возможно, вам понадобится аннотация @XmlSeeAlso.

Учитывая следующее:

public class A { 

} 

и:

import javax.xml.bind.annotation.*; 

@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER) 
@XmlType(propOrder = {"obj"}) 
@XmlRootElement(name = "response") 
public class B<T extends A> extends A { 

    private T obj; 

    @XmlElement(required = true) 
    public T getObj() { 
    return obj; 
    } 

    public void setObj(T obj) { 
     this.obj = obj; 
    } 

} 

Когда я бегу:

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(B.class); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     B b = new B(); 
     b.setObj(new A()); 
     marshaller.marshal(b, System.out); 

    } 

} 

я получаю:

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

И когда я бегу:

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(B.class); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     B b = new B(); 
     b.setObj(new B()); 
     marshaller.marshal(b, System.out); 

    } 

} 

я получаю:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<response> 
    <obj xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="b"/> 
</response> 
+0

я пытаюсь выстроить экземпляр следующего класса: общественного класса B расширяет { ... } WHERE C является подклассом A – roman

+0

Я обновил свой ответ, это может быть связано с созданием вашего JAXBContext. –

+0

эй Блейз, я не мог разобрать, что вызывает пространство имен во втором. Я столкнулся с аналогичной проблемой, когда дженерики печатаются с помощью имениpix (info), а конкретные классы - просто прекрасные, красивые xml. Можете ли вы помочь мне, как это сделать? Кстати, я использую аннотацию и попытался указать @XmlSchema на уровне pacjkage и типа, но безуспешно. –

Смежные вопросы