我有以下Enum
课程:
@XmlJavaTypeAdapter(value = PastMedicalHistoryAdapter.class)
public enum PastMedicalHistory {
Diabetes, Obesity, Smoking, COPD, CAD, PVD, Other
}
和通用适配器:
public abstract class GenericEnumAdapter<T extends Enum> extends XmlAdapter<String, Enum> {
@Override
public Enum unmarshal(String v) throws Exception {
log.info("unmarshal: {}", v);
return convert(v + "");
}
public abstract T convert(String value);
@Override
public String marshal(Enum v) throws Exception {
log.info("marshal: {}", v.name());
String s = "{\"" + v.name() + "\":" + true + "}";
return s;
}
}
和基本实现
public class PastMedicalHistoryAdapter extends GenericEnumAdapter<PastMedicalHistory> {
@Override
public PastMedicalHistory convert(String value) {
return PastMedicalHistory.valueOf(value);
}
}
我像这样使用它:
@Data
@XmlRootElement(name = "Patient")
public class Test {
private List<PastMedicalHistory> history;
public static void main(String[] args) throws Exception {
JAXBContext cxt = JAXBContext.newInstance(Test.class);
Marshaller mar = cxt.createMarshaller();
mar.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
mar.setProperty(JAXBContextProperties.MEDIA_TYPE, "application/json");
mar.setProperty(JAXBContextProperties.JSON_INCLUDE_ROOT, Boolean.FALSE);
Test t = new Test();
t.setHistory(Arrays.asList(PastMedicalHistory.CAD, PastMedicalHistory.Diabetes));
mar.marshal(t, System.out);
}
}
问题是历史的输出始终为空,如下所示:
[exec:exec]
2013-09-29 12:13:18:511 INFO marshal: CAD
2013-09-29 12:13:18:522 INFO marshal: Diabetes
{
"history" : [ null, null ]
}
我使用 Moxy2.5.1
作为JAXB
提供者,所以我错过了什么,或者我做错了什么?