简体   繁体   中英

Incorrect unmarshalling result with subclasses

My unmarshalling result is wrong for element Root. It contains a reference to its own subclass to use the key as attribute and it has a reference to another subclass. I'm using MOXy version 2.7.0 and jdk8u45.

I create a root element like this:

Root root = new Root("rootKey", "subKey");

Marshalling results in correct xml output:

<root name="rootKey"><sub name="subKey"/></root>

Unmarshalling this xml results in an incorrect object. The resulting object has "subKey" for both the key names. Is something wrong with my setup or is this a bug?

The Root class looks like this:

@XmlRootElement(name = "root")
public class Root {
    @XmlPath(".")
    private final Root.Key key;

    @XmlElement(name = "sub")
    private final Sub.Key subKey;

    public Root(String name, String subName) {
        this.key = new Root.Key(name);
        this.subKey = new Sub.Key(subName);
    }

    private Root() {
        this(null, null);
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((key == null) ? 0 : key.hashCode());
        result = prime * result + ((subKey == null) ? 0 : subKey.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj != null && obj.getClass().equals(this.getClass())) {
            Root o = (Root)obj;
            return key.equals(o.key) && subKey.equals(o.subKey);
        }
        return false;
    }

    @Override
    public String toString() {
        return "Root [key=" + key + ", subKey=" + subKey + "]";
    }

    @XmlType(name = "Root.Key")
    @XmlRootElement
    public static final class Key {
        @XmlAttribute
        private final String name;

        public Key(String name) {
            this.name = name;
        }

        private Key() {
            // Constructor for JAXB
            this(null);
        }

        public String getName() {
            return name;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((name == null) ? 0 : name.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj != null && obj.getClass().equals(this.getClass())) {
                Key o = (Key)obj;
                return name.equals(o.name);
            }
            return false;
        }

        @Override
        public String toString() {
            return "Key [name=" + name + "]";
        }
    }
}

The Sub class is:

@XmlRootElement(name = "sub")
public class Sub {
    @XmlType(name = "Sub.Key")
    @XmlRootElement
    public static final class Key {
        @XmlAttribute
        private final String name;

        public Key(String name) {
            this.name = name;
        }

        private Key() {
            // Constructor for JAXB
            this(null);
        }

        public String getName() {
            return name;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((name == null) ? 0 : name.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj != null && obj.getClass().equals(this.getClass())) {
                Key o = (Key)obj;
                return name.equals(o.name);
            }
            return false;
        }

        @Override
        public String toString() {
            return "Key [name=" + name + "]";
        }
    }
}

Try this:

Your Root class

@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
    @XmlAttribute(name = "name")
    private final Root.Key key;
    @XmlElement(name = "sub")
    private final Sub.Key subKey;

    public Root(String name, String subName) {
        this.key = new Root.Key(name);
        this.subKey = new Sub.Key(subName);
    }

    private Root() {
        this(null, null);
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((key == null) ? 0 : key.hashCode());
        result = prime * result + ((subKey == null) ? 0 : subKey.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj != null && obj.getClass().equals(this.getClass())) {
            Root o = (Root)obj;
            return key.equals(o.key) && subKey.equals(o.subKey);
        }
        return false;
    }

    @Override
    public String toString() {
        return "Root [key=" + key + ", subKey=" + subKey + "]";
    }

    @XmlType(name = "Root.Key")
    @XmlRootElement
    public static final class Key {
        @XmlValue
        private final String name;

        public Key(String name) {
            this.name = name;
        }

        public Key() {
            // Constructor for JAXB
            this(null);
        }

        public String getName() {
            return name;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((name == null) ? 0 : name.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj != null && obj.getClass().equals(this.getClass())) {
                Key o = (Key)obj;
                return name.equals(o.name);
            }
            return false;
        }

        @Override
        public String toString() {
            return "Key [name=" + name + "]";
        }
    }
}

And the Sub class

@XmlAccessorType(XmlAccessType.FIELD)
public class Sub {

    @XmlType(name = "Sub.Key")
    public static final class Key {
        @XmlAttribute(name = "name")
        private final String name;

        public Key(String name) {
            this.name = name;
        }

        public Key() {
            // Constructor for JAXB
            this(null);
        }

        public String getName() {
            return name;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((name == null) ? 0 : name.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj != null && obj.getClass().equals(this.getClass())) {
                Key o = (Key)obj;
                return name.equals(o.name);
            }
            return false;
        }

        @Override
        public String toString() {
            return "Key [name=" + name + "]";
        }
    }
}

and you should be able to marshal/unmarshal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM