JAXB枚举字段未被序列化

时间:2015-01-18 06:26:30

标签: java enums jaxb xml-serialization

我有以下课程:

package dictionary;

import java.io.Serializable;
import java.util.Objects;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlEnum;
import javax.xml.bind.annotation.XmlTransient;

public class Definition implements Serializable {

        private static final long serialVersionUID = 1L;

        @XmlEnum
        public enum GrammaticalCategory {
                MALE("m."),
                FEMALE("f."),
                ADJECTIVE("adj."),
                ALSO_NOUN("Ú. t. c. s."),
                ALSO_PLURAL("Ú. t. en pl."),
                MORE_PLURAL("Ú. m. en pl."),
                ADVERB("adv."),
                ARTICLE("art."),
                CONJUNCTION("conj."),
                PRONOMINAL_VERB("prnl."),
                FIGURATIVE_PHRASE("fr. fig."),
                ADVERBIAL_PHRASE("loc. adv."),
                CONJUNCTIVE_PHRASE("loc. conj."),
                TRANSITIVE_VERB("tr."),
                INTRANSITIVE_VERB("intr."),
                ALSO_PRONOMINAL("ú. t. c. prnl."),
                INTERJECTION("interj."),
                ONOMATOPEIA("onom.");

                // Category abbreviation in spanish.
                @XmlTransient
                public final String esAbbrev;

                GrammaticalCategory(String esAbbrev) {
                        this.esAbbrev = esAbbrev;
                }

                /*
                 * Returns the GrammaticalCategory object determined by its spanish abbreviation.
                 * Returns null if esAbbrev == null or the spanish abbreviation is unknown.
                 */
                public static GrammaticalCategory fromEsAbbrev(String esAbbrev) {
                        if (esAbbrev == null) {
                                return null;
                        }

                        for (GrammaticalCategory cat : values()) {
                                if (cat.esAbbrev.equalsIgnoreCase(esAbbrev)) {
                                        return cat;
                                }
                        }

                        return null;
                }
        }

        private String definition;

        private GrammaticalCategory category;

        public Definition() {
                this("");
        }

        public Definition(String string) throws IllegalArgumentException {
                this(string, null);
        }

        public Definition(String string, GrammaticalCategory category) {
                if (string == null) {
                        throw new IllegalArgumentException("string for the definition cannot be null");
                }
                this.definition = string;
                this.category = category;
        }

        @XmlElement(name = "definition")
        public String getDefinition() {
                return definition;
        }

        public void setDefinition(String definition) {
                this.definition = definition;
        }

        // Returns the category of the definition, or null if there is no category associated.
        @XmlElement(name = "cat")
        public GrammaticalCategory getCategory() {
                return category;
        }

        public void setCategory(GrammaticalCategory cat) {
                category = cat;
        }

        public String toString() {
                return "Definition[definition=" + definition + (category != null ? ",cat=" + category.name() : "");
        }

        public boolean equals(Object o) {
                if (o instanceof Definition) {
                        Definition other = (Definition) o;
                        return definition.equals(definition) && Objects.equals(category, other.category);
                }

                return false;
        }
}

我正在尝试使用JAXB序列化它。枚举字段没有被写入。为什么?

我在枚举中注释了@XmlTransient字符串字段,因为我只想要枚举枚举的名称。

1 个答案:

答案 0 :(得分:2)

Java中的

Enum意味着是不可变对象,在这种情况下,序列化任何字段都没有意义。

JAXB仅通过写出他们的名字来序列化enum,没有别的。您不需要在@XmlTransient的字段上使用enum注释,即使您省略它们也不会被序列化。

无论有没有@XmlTransient

,我的班级都会为我正确地序列化

序列化代码:

Definition d = new Definition();
d.setCategory(GrammaticalCategory.ADJECTIVE);
d.setDefinition("testdefinition");

JAXB.marshal(d, new File("out.xml"));

<强>输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definition>
    <cat>ADJECTIVE</cat>
    <definition>testdefinition</definition>
</definition>

我的猜测是你没有使用setCategory()方法设置类别,如果属性是null,它将不会出现在输出XML中。