2

我有以下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提供者,所以我错过了什么,或者我做错了什么?

4

1 回答 1

2

注意: 我是EclipseLink JAXB (MOXy)负责人,也是JAXB (JSR-222)专家组的成员。

我已经能够重现您遇到的错误。您可以使用以下链接跟踪我们在此问题上的进展:

于 2013-10-02T10:05:23.157 回答