我的代码可以运行,但是我的Feed
对象不包含任何值。
我的XML:
<feed xmlns="http://www.w3.org/2005/Atom">
<title>All last builds only</title>
<updated>2019-03-11T11:32:16Z</updated>
<author>
<name>Jenkins Server</name>
</author>
<id>urn:uuid:903deee0-7bfa-11db-9fe1-0800200c9a88</id>
<entry>
<title>integration-tests #12 (14 less tests are failing (total 4))</title>
<link rel="alternate" type="text/html" href="http://localhost/jenkins/job/integration-tests/12/" />
<id>tag:hudson.dev.java.net,2008:http://localhost/jenkins/job/integration-tests/</id>
<published>2019-03-11T11:32:16Z</published>
<updated>2019-03-11T11:32:16Z</updated>
</entry>
<entry>
<title>NetworkTest #2 (back to normal)</title>
<link rel="alternate" type="text/html" href="http://localhost/jenkins/job/NetworkTest/2/" />
<id>tag:hudson.dev.java.net,2008:http://localhost/jenkins/job/NetworkTest/</id>
<published>2019-03-11T08:09:59Z</published>
<updated>2019-03-11T08:09:59Z</updated>
</entry>
<entry>
<title>Release ng-back #3 - ng-back - Release 1.0.0 (stable)</title>
<link rel="alternate" type="text/html" href="http://localhost/jenkins/job/Release%20ng-back/3/" />
<id>tag:hudson.dev.java.net,2008:http://localhost/jenkins/job/Release%20ng-back/</id>
<published>2019-02-06T17:39:54Z</published>
<updated>2019-02-06T17:39:54Z</updated>
</entry>
<entry>
<title>Release ng-front #1 - Release 1.0.0 (stable)</title>
<link rel="alternate" type="text/html" href="http://localhost/jenkins/job/Release%20ng-front/1/" />
<id>tag:hudson.dev.java.net,2008:http://localhost/jenkins/job/Release%20ng-front/</id>
<published>2019-02-08T10:43:59Z</published>
<updated>2019-02-08T10:43:59Z</updated>
</entry>
</feed>
我的Java代码:
public static void main(String[] args) {
//
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://localhost/jenkins/rssLatest").build();
Response response = client.newCall(request).execute();
String a = response.body().string();
String xml = a.trim();
System.out.println(xml);
JAXBContext jaxbContext = JAXBContext.newInstance(Feed.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(xml);
Feed feed = (Feed) unmarshaller.unmarshal(reader);
System.out.println(feed.getTitle());
response.body().close();
} catch (IOException | JAXBException e) {
System.err.println(e);
}
}
Feed Pojo:
@XmlRootElement(namespace="http://www.w3.org/2005/Atom")
public class Feed {
private String title;
Link LinkObject;
private String updated;
Author AuthorObject;
private String id;
ArrayList<Object> entry = new ArrayList<>();
private String _xmlns;
// Getter Methods
public String getTitle() {
return title;
}
public Link getLink() {
return LinkObject;
}
public String getUpdated() {
return updated;
}
public Author getAuthor() {
return AuthorObject;
}
public String getId() {
return id;
}
public String get_xmlns() {
return _xmlns;
}
// Setter Methods
public void setTitle(String title) {
this.title = title;
}
public void setLink(Link linkObject) {
this.LinkObject = linkObject;
}
public void setUpdated(String updated) {
this.updated = updated;
}
public void setAuthor(Author authorObject) {
this.AuthorObject = authorObject;
}
public void setId(String id) {
this.id = id;
}
public void set_xmlns(String _xmlns) {
this._xmlns = _xmlns;
}
}
控制台:
null
答案 0 :(得分:0)
我认为需要更多的JAXB注释。 你可以试试这个吗?
@XmlRootElement(name = "feed", namespace="http://www.w3.org/2005/Atom")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"title",
"updated",
"author",
"id",
"entry"
})
public class Feed {
@XmlElement(name = "title")
private String title;
Link LinkObject;
@XmlElement(name = "updated")
private String updated;
@XmlElement(name = "author")
Author AuthorObject;
@XmlElement(name = "id")
private String id;
@XmlElement(name = "entry")
ArrayList<Object> entry = new ArrayList<>();
private String _xmlns;
// Getter Methods
}
文档: