我在使用Moxy和JSON进行自定义枚举编组方面遇到了一些麻烦。我的用例是我有一个大对象模型,其中包含通常应提供正常枚举值的枚举,"代码"和描述。这些数据的来源只有"代码",所以我需要能够仅使用代码来解组这些枚举的实例(例如
{"companyCode":{"code":"PI"}}
。
但是,我也应该能够整理和解组所有三个领域:
{"companyCode":
{"value":"Private",
"code":"PI","description":
"Private Ins"
}
}
我正在使用看起来像这样的适配器:
public class CodeEnumXmlAdapter<E extends Enum<E> & CodeEnum> extends XmlAdapter<CodeEnumImpl,E> {
public static <T extends Enum<T> & CodeEnum> T getFromName(Class<T> clazz, String name) {
if (name == null) return null;
T[] values = clazz.getEnumConstants();
for (T t : values) {
if (name.equals(t.name())) {
return t;
}
}
return null;
}
public static <T extends Enum<T> & CodeEnum> T getFromCode(Class<T> clazz, String code) {
if (code == null) return null;
T[] values = clazz.getEnumConstants();
for (T t : values) {
if (code.equals(t.getCode())) {
return t;
}
}
return null;
}
public static <T extends Enum<T> & CodeEnum> T getFromString(Class<T> clazz, String aString) {
if (aString == null) return null;
T[] values = clazz.getEnumConstants();
for (T t : values) {
if (aString.equals(t.getCode()) || aString.equals(t.name()) || aString.equals(t.getDescription())) {
return t;
}
}
return null;
}
@Override
public E unmarshal(CodeEnumImpl value) throws Exception {
if (value == null) return null;
String valueString = value.getValue();
if (valueString == null)
valueString = value.getCode();
if (valueString == null)
valueString = value.getDescription();
if (valueString == null)
return null;
Type generic = ((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
return getFromString((Class<E>)generic, valueString);
}
@Override
public CodeEnumImpl marshal(E value) throws Exception {
return value == null ? null : new CodeEnumImpl(value);
}
}
这会从这样的枚举中转换出来:
import org.apache.commons.lang3.StringUtils;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlJavaTypeAdapter(CompanyCode.Adapter.class)
public enum CompanyCode implements CodeEnum {
// Changed "Commmercial" to "Client" based on inputs from ...Greg, Tamil
Client("CM", "Client"), Medicare("MC", "Medicare"), Medicaid("MD",
"Medicaid"), Private("PI", "Private Ins"), Patient("PT", "Patient");
private String code;
private String description;
private CompanyCode(String code, String label) {
this.code = code;
this.description = label;
}
public String getDescription() {
return description;
}
public String getCode() {
return code;
}
public static CompanyCode fromCode(String code) {
if (StringUtils.isEmpty(code)) {
return null;
}
for (CompanyCode freq : values()) {
if (freq.getCode().equalsIgnoreCase(code)) {
return freq;
}
}
throw new IllegalArgumentException("Invalid CompanyCode code: " + code);
}
public String toString() {
return description;
}
public static class Adapter extends CodeEnumXmlAdapter<CompanyCode> {}
}
并使用和这样的中间类型:
import javax.xml.bind.annotation.XmlElement;
/**
* Created by Jeffrey Hoffman on 6/24/2015.
*/
public class CodeEnumImpl {
String value;
String description;
String code;
public CodeEnumImpl() {
}
public <E extends Enum<E> & CodeEnum> CodeEnumImpl(E value) {
if (value != null) {
this.value = value.name();
this.description = value.getDescription();
this.code = value.getCode();
}
}
@XmlElement
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@XmlElement
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@XmlElement
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Override
public String toString() {
return value == null ? null : value.toString();
}
}
这适用于直接XML和JAXB。但是,当我尝试使用Moxy时,我得到一个像这样的例外:
异常描述:类[类的私有Ins] com.labcorp.phoenix.biz.enums.CompanyCode],无法转换为 [类java.lang.Object]。内部例外:例外 [EclipseLink-115](Eclipse持久性服务 - 2.5.0.v20130507-3faac2b):org.eclipse.persistence.exceptions.DescriptorException异常 描述:没有为属性[Private]提供转换值。 制图: org.eclipse.persistence.oxm.mappings.XMLDirectMapping [企业编码 - &GT;企业编码/文本()] 描述符:XMLDescriptor(com.labcorp.phoenix.eligibility.Root - &gt; [DatabaseTable(root)])at org.eclipse.persistence.exceptions.ConversionException.couldNotBeConverted(ConversionException.java:87) 在 org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter.convertObjectValueToDataValue(XMLJavaTypeConverter.java:178) 在 org.eclipse.persistence.oxm.mappings.XMLDirectMapping.convertObjectValueToDataValue(XMLDirectMapping.java:511) 在 org.eclipse.persistence.oxm.mappings.XMLDirectMapping.getFieldValue(XMLDirectMapping.java:330) 在 org.eclipse.persistence.internal.oxm.XMLDirectMappingNodeValue.marshalSingleValue(XMLDirectMappingNodeValue.java:62) 在 org.eclipse.persistence.internal.oxm.XMLDirectMappingNodeValue.marshal(XMLDirectMappingNodeValue.java:58) 在 org.eclipse.persistence.internal.oxm.NodeValue.marshal(NodeValue.java:102) 在 org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext.marshal(ObjectMarshalContext.java:59) 在 org.eclipse.persistence.internal.oxm.XPathNode.marshal(XPathNode.java:393) 在 org.eclipse.persistence.internal.oxm.XPathNode.marshal(XPathNode.java:368) 在 org.eclipse.persistence.internal.oxm.XPathObjectBuilder.buildRow(XPathObjectBuilder.java:238) 在 org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:118) 在 org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:1) 在 org.eclipse.persistence.internal.oxm.XMLMarshaller.marshal(XMLMarshaller.java:743) 在 org.eclipse.persistence.internal.oxm.XMLMarshaller.marshal(XMLMarshaller.java:1124) 在 org.eclipse.persistence.internal.oxm.XMLMarshaller.marshal(XMLMarshaller.java:869) ... 7更多引起:异常[EclipseLink-115](Eclipse 持久性服务 - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DescriptorException
这似乎是moxy中的一个错误,因为我的适配器会转换为非枚举类型,所以不应该有一个处理枚举的nestedConverter。
答案 0 :(得分:2)
我设法用2.5.0重现你的问题。它最可能已经修复过的bug。无法在Eclipse Bugzilla中找到错误,但相同的代码可以在2.6.0中正常运行。你能升级到最新的MOXy吗?