我有以下类(class):
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 对其进行序列化。枚举字段未被写入。为什么?
我已将枚举内的 String 字段注释为 @XmlTransient
,因为我只想序列化枚举的名称。
最佳答案
Java 中的
Enum
意味着是不可变的对象,在这种情况下,序列化它们的任何字段都是没有意义的。
JAXB 仅通过写出枚举的名称来序列化枚举,仅此而已。您不需要在 enum
的字段上使用 @XmlTransient
注释,即使省略它,它们也不会被序列化。
您的类已为我正确序列化,无论是否带有 @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 中.
关于java - JAXB 枚举字段未序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28007713/