gpt4 book ai didi

java jaxb 使用 jenkins Feed 对象进行解码

转载 作者:太空宇宙 更新时间:2023-11-04 09:48:37 26 4
gpt4 key购买 nike

我的代码已运行,但我的 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);
}
}

喂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

最佳答案

我认为需要更多 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
}

文档:

关于java jaxb 使用 jenkins Feed 对象进行解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55102724/

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