java XML解析返回40个IllegalAnnotationException计数
我试图解析对象的XML响应,但它引发异常
响应链接为this:
<response>
<meta>
<per_page>10</per_page>
<total>20</total>
<geolocation>None</geolocation>
<took>8</took>
<page>1</page>
</meta>
<events>
<event>
...
</event>
<event>
...
</event>
....
</events>
</response>
代码
queryString = queryString.replaceAll(" ", "%20");
try {
URL page = new URL(queryString);
HttpURLConnection conn = (HttpURLConnection) page.openConnection();
conn.connect();
InputStreamReader in = new InputStreamReader(conn.getInputStream(),Charset.forName("UTF-8"));
this.response = (Response) JAXB.unmarshal(in, Response.class);
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
例外情况
javax.xml.bind.DataBindingException: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 40
counts of IllegalAnnotationExceptions
Class has two properties of the same name "events"
this problem is related to the following location:
....
对象类
@XmlRootElement(name = "Response")
public class Response {
@XmlElement(name="per_page")
private int per_page;
@XmlElement(name="total")
private int total;
@XmlElement(name="geolocation")
private String geolocation;
@XmlElement(name="took")
private int took;
@XmlElement(name="page")
private int page;
@XmlElement(name="events")
private List<Event> events = null;
**getters and setters**
对象
@XmlRootElement(name="event")
public class Event {
@XmlElement(name = "links")
private String link;
@XmlElement(name = "id")
private int id;
@XmlElement(name = "stats")
private Stats stats;
@XmlElement(name = "title")
private String title;
@XmlElement(name = "announce_date")
private String announce_date;
@XmlElement(name = "score")
private float score;
@XmlElement(name = "date_tbd")
private boolean date_tbd;
@XmlElement(name = "type")
private String type;
@XmlElement(name = "datetime_local")
private String datetime_local;
@XmlElement(name = "visible_until_utc")
private String visible_util_utc;
@XmlElement(name = "time_tbd")
private boolean time_tbd;
@XmlElement(name = "taxonomies")
private List<Taxonomie> taxonomies;
@XmlElement(name = "performers")
private List<Performer> performers;
@XmlElement(name = "url")
private String url;
@XmlElement(name = "created_at")
private String created_at;
@XmlElement(name = "venue")
private Venue venue;
@XmlElement(name = "short_title")
private String short_title;
@XmlElement(name = "datetime_utc")
private String datetime_utc;
@XmlElement(name = "datetime_tbd")
private boolean datetime_tbd;
**getters and setters**
# 1 楼答案
默认情况下,JAXB实现将公共字段和属性视为映射。注释非公共字段时,该字段也将被映射。然后,如果您有一个映射字段和一个同名属性,您将得到此异常
在注释字段时,需要使用
@XmlAccessorType(XmlAccessType.FIELD)
注释类注意:
您当前在模型上添加的注释超过了需要的数量。由于JAXB是异常配置,您只需要在希望XML表示不同于默认值的地方添加注释