gpt4 book ai didi

java - JAXB 枚举字段未被序列化

转载 作者:行者123 更新时间:2023-11-30 08:57:42 25 4
gpt4 key购买 nike

我有以下类(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 对其进行序列化。未写入枚举字段。为什么?

我已将枚举中的字符串字段注释为 @XmlTransient,因为我只想序列化枚举的名称。

最佳答案

Java 中的

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

JAXB 序列化 enum 仅通过写出它们的名字,没有别的。您不需要在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com