我正在尝试正确地注释我的类,所以我可以将SimpleXML与android spring框架一起使用。我的问题是,我总是得到:
E/something(25192): Could not read [class at.something.scanner.Issues]; nested exception is org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.ElementList(data=false, empty=true, entry=, inline=true, name=issues, required=true, type=void) on field 'issues' private java.util.List at.something.scanner.Issues.issues for class at.something.scanner.Issues at line 1
和
E/something(25192): Caused by: org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.ElementList(data=false, empty=true, entry=, inline=true, name=issues, required=true, type=void) on field 'issues' private java.util.List at.something.scanner.Issues.issues for class at.something.scanner.Issues at line 1
我知道我可以通过使用@ElementList(required = false)来消除ValueRequiredException,但它并没有解决我的问题,因为我最终得到一个空列表。
请求使用spring:
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
Serializer serializer = new Persister(new Format("<?xml version=\"1.0\" encoding= \"UTF-8\" ?>"));
restTemplate.getMessageConverters().add(new SimpleXmlHttpMessageConverter(serializer));
ResponseEntity<Issues> response = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<Object>(requestHeaders), Issues.class);
Issues tickets = response.getBody();
Issues.java:
@Root
public class Issues{
@ElementList(name="issues", inline=true)
private List<Issue> issues;
public void setIssues(List<Issue> results) {
this.issues = results;
}
public List<Issue> getIssues() {
return issues;
}
}
Issue.java
@Root(name="issue")
public class Issue{
@Attribute(name="id")
private String id;
}
来自服务器的XML响应示例:
<issues>
<issue id="ABC-10">
<field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="SingleField" name="Assignee">
<value>someone</value>
</field>
</issue>
<issue id="ABC-11">
<field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="SingleField" name="reporterName">
<value>someone</value>
</field>
</issue>
</issues>
我希望有人可以给我一个如何解决我的问题的提示。
答案 0 :(得分:1)
使用
@ElementList(name="issues", inline=true, entry="Issue")
我有同样的问题,这对我有用。在Stackoverflow上找到了这个答案。
修改强>
我已经尝试过使用Simple XML和类Issue的示例 你有
@Element(name="field")
private Field field;
丢失。
public class Field {
@Element(name = "value", required = true)
private String value;
@Attribute(name = "name", required = false)
private String name;
}
我也用过
public static void main(String args[]) throws Exception {
Serializer serializer = new Persister(new Format("<?xml version=\"1.0\" encoding= \"UTF-8\" ?>"));
File source = new File("sample.xml");
Issues i = serializer.read(Issues.class, source);
}
我没有错误。使用xml中的数据正确加载了对象。