JAXB有没有办法对此给出警告。。。子类中重写的属性?

JAXB有没有办法对此给出警告。。。子类中重写的属性?,jaxb,Jaxb,我遇到了一个有趣的JAXB问题,这让我困惑了一段时间,因为没有产生错误,只是没有看到我期望的输出 我们有几个类都扩展了基类。 有一次,我在基类中引入了一个对象属性,该属性恰好也是在其中一个子类中定义的属性。这导致来自任何子类(覆盖该属性的子类之外)的该属性的任何setter在XML输出中完全忽略该属性 例如: @XmlRootElement(name = "animal") @XmlAccessorType(XmlAccessType.FIELD) @XmlSeeAlso({Dog.class,

我遇到了一个有趣的JAXB问题,这让我困惑了一段时间,因为没有产生错误,只是没有看到我期望的输出

我们有几个类都扩展了基类。 有一次,我在基类中引入了一个对象属性,该属性恰好也是在其中一个子类中定义的属性。这导致来自任何子类(覆盖该属性的子类之外)的该属性的任何setter在XML输出中完全忽略该属性

例如:

@XmlRootElement(name = "animal")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({Dog.class, Fish.class})
public class Animal {
    protected String movement;
     ....
}

@XmlRootElement(name = "dog")
@XmlSeeAlso({Animal.class})
@XmlAccessorType(XmlAccessType.FIELD)
public class Dog extends Animal {
   private String breed;
   protected String movement;
   ....
}

@XmlRootElement(name = "fish")
@XmlSeeAlso({Animal.class})
@XmlAccessorType(XmlAccessType.FIELD)
public class Fish extends Animal {
    private String scaleType;
    ....
}

public static void main(String[] args) {
    Dog dog = new Dog();
    dog.setBreed("lab");
    dog.setMovement("walks"); 
    String xml = AnimalJaxb.toXml(dog);
    System.out.println("dog = "+xml);

    Fish fish = new Fish();
    fish.setMovement("swims"); //WILL NOT SHOW UP IN XML!
    fish.setScaleType("normal"); 
    String xml = AnimalJaxb.toXml(fish);
    System.out.println("fish = "+xml); 


 //to and from XML looks like...
    public static  <T> T fromXml(String xml, Class<T> clazz) throws Exception {
    ByteArrayInputStream input = null;
    Unmarshaller u = null;
    try {
        input = new ByteArrayInputStream(xml.getBytes());
        JAXBContext jc = JAXBContext.newInstance(clazz);
        u = jc.createUnmarshaller();
    } catch (Exception e) {
        throw e;
    } finally {
        input.close();
    }
    return (T) u.unmarshal(input);
}

public  static  String toXml(Object obj) throws Exception {

    StringWriter sw = new StringWriter();

    try {
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(obj, sw);

        return sw.toString();

    } catch (Exception e) {
        throw e;
    } finally {
        sw.close();
    }
}
@XmlRootElement(name=“animal”)
@XmlAccessorType(XmlAccessType.FIELD)
@XMLSEEALLO({Dog.class,Fish.class})
公营动物{
保护绳运动;
....
}
@XmlRootElement(name=“dog”)
@XMLSEEAL({Animal.class})
@XmlAccessorType(XmlAccessType.FIELD)
公家犬{
私家犬;
保护绳运动;
....
}
@XmlRootElement(name=“fish”)
@XMLSEEAL({Animal.class})
@XmlAccessorType(XmlAccessType.FIELD)
公共级鱼类和动物{
私有字符串scaleType;
....
}
公共静态void main(字符串[]args){
狗=新狗();
狗狗。狗狗(简称“实验室”);
狗的运动(“行走”);
字符串xml=AnimalJaxb.toXml(dog);
System.out.println(“dog=“+xml”);
鱼=新鱼();
fish.setMovement(“游泳”);//将不会在XML中显示!
fish.setScaleType(“正常”);
字符串xml=AnimalJaxb.toXml(fish);
System.out.println(“fish=“+xml”);
//从XML到XML看起来像。。。
公共静态T fromXml(字符串xml,类clazz)引发异常{
ByteArrayInputStream输入=null;
解组器u=null;
试一试{
input=newbytearrayinputstream(xml.getBytes());
JAXBContext jc=JAXBContext.newInstance(clazz);
u=jc.createUnmarshaller();
}捕获(例外e){
投掷e;
}最后{
input.close();
}
返回(T)u.unmarshal(输入);
}
公共静态字符串toXml(对象obj)引发异常{
StringWriter sw=新的StringWriter();
试一试{
JAXBContext context=JAXBContext.newInstance(obj.getClass());
Marshaller m=context.createMarshaller();
m、 setProperty(Marshaller.JAXB_格式的_输出,Boolean.TRUE);
m、 元帅(obj,sw);
返回sw.toString();
}捕获(例外e){
投掷e;
}最后{
sw.close();
}
}
当上述操作运行时,Fish上的“movement”属性将不在XML中。要解决这个问题,我需要删除Dog类中重写的movement属性


令人沮丧的是,JAXB没有抛出任何错误或抱怨。它抱怨的唯一方式是将基类(动物)抽象并标记为@XmlTransient,之后JAXB会抱怨具有“两个同名属性”

我无法重现您的问题。使用JDK 1.7的代码,鱼的移动已被保存。您确定您在AnimalJaxb中正确启动了JAXBContext吗?但是,如果没有在狗身上覆盖移动的setter,狗的移动没有被序列化,但是我会责怪字段隐藏。嗨,Zielu,我刚刚添加了to和from xml代码,wh在我创建JAXBContext之前。