@XmlType(name = "WeekEnum")
@XmlEnum
public enum WeekEnum implements Serializable {
MONDAY("Monday"),
@XmlEnumValue("SUNDAY.ON")
SUNDAY("Sunday")
private final String value;
WeekEnum(String v) {
value = v;
}
public String value() {
return value;
}
public static WeekEnum fromValue(String v) {
for (WeekEnum c: WeekEnum.values()) {
if (c.value.equals(v)) {
return c;
}
}
throw new IllegalArgumentException(v);
}
}
我有一个Enum类WeekEnum,它是一个Jaxb类。
WeekEnum day = WeekEnum.SUNDAY;
为了从Enum日获得值“Sunday”,我们写day.value();
要从Enum日获得值“SUNDAY”,我们写day.name();
我们如何从Eum日获得“SUNDAY.ON”的价值?
如果我有“星期日”的价值,我们如何获得相应的“SUNDAY.ON”?
您需要将其存储在与值类似的单独变量中。
@XmlType(name = "WeekEnum")
@XmlEnum
public enum WeekEnum implements Serializable {
MONDAY("Monday"),
@XmlEnumValue("SUNDAY.ON")
SUNDAY("Sunday", "SUNDAY.ON")
private final String value;
private final String xmlValue;
WeekEnum(String v) {
value = v;
}
WeekEnum(String v, String xmlValue){
this(v);
this.xmlValue = xmlValue;
}
public String value() {
return value;
}
public String getXmlValue() {
return xmlValue;
}
public static WeekEnum fromValue(String v) {
for (WeekEnum c: WeekEnum.values()) {
if (c.value.equals(v)) {
return c;
}
}
throw new IllegalArgumentException(v);
}
}
如果您有一个名为week的枚举实例,则可以执行以下操作
week.getClass().getField(week.name()).getAnnotation(XmlEnumValue.class).value()