如何解决JAXB解组列表以映射适配器
<?xml version='1.0' encoding='UTF-8' standalone='no' ?>
<Result>
<Module name="module">
<TestCase name="testCase1"></TestCase>
<TestCase name="testCase2"></TestCase>
<TestCase name="testCase3"></TestCase>
</Module>
</Result>
我想将TestCase的列表转换为 HashMap 。
Result.java
@XmlRootElement(name = "Result")
public class Result {
@XmlElement(name = "Module")
private List<Module> modules;
}
Module.java
public class Module {
@XmlAttribute
public String name;
@XmlJavaTypeAdapter(TestCaseAdapter.class)
@XmlElement(name = "TestCase")
private Map<String,TestCase> testCases;
}
TestCase.java
public class TestCase {
@XmlAttribute
private String name;
}
TestCaseAdapter.java
public class TestCaseAdapter extends XmlAdapter<List<TestCase>,Map<String,TestCase>> {
@Override
public Map<String,TestCase> unmarshal(List<TestCase> testCases) throws Exception {
Map<String,TestCase> map = new HashMap<>();
for (TestCase testCase : testCases) {
map.put(testCase.getName(),testCase);
}
return map;
}
@Override
public List<TestCase> marshal(Map<String,TestCase> testCaseMap) throws Exception {
return new ArrayList<>(testCaseMap.values());
}
}
但是,以上代码无法正常运行。执行代码时,会出现以下错误。
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
java.util.List is an interface,and JAXB can't handle interfaces.
this problem is related to the following location:
at java.util.List
at private java.util.Map com.nebula.parser.model.Module.testCases
at com.nebula.parser.model.Module
at private java.util.List com.nebula.parser.model.Result.modules
at com.nebula.parser.model.Result
因此,我更改了以下代码,并且效果很好。但 TestCase []为空。
public class TestCaseAdapter extends XmlAdapter<TestCase[],TestCase> unmarshal(TestCase[] testCases) throws Exception {
return null;
}
@Override
public TestCase[] marshal(Map<String,TestCase> testCaseMap) throws Exception {
return new TestCase[0];
}
}
我想解决这个问题,但我无法解决。如果您能帮助我,我会很感激。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。