gpt4 book ai didi

java - JAXB 中的 XmlAdaptar

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

我有以下 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 ]
}

我正在使用 Moxy 2.5.1 作为 JAXB 提供程序,那么我缺少了什么,或者我做错了什么?

最佳答案

注意:我是 EclipseLink JAXB (MOXy) JAXB (JSR-222) 的领导和成员专家组。

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

关于java - JAXB 中的 XmlAdaptar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19076339/

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