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

标签 java jaxb

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

相关文章:

OptionPane 中显示的 Java 多维数组

java - 使用 JPA 提高 foreach 持久调用的性能

使用 websocket 作为传输的基于 Java 的 JMS 框架

java - JAXB 解码 XML 元素以映射

xml - xjc 未按预期生成列表

java - jaxb2-basics是否可以配置为泛型对象的类型变量指定接口(interface)?

java - 使用 Jaxab SchemaGen

java - 如何用不同的泛型类型实例化基类?

Java同步和访问静态字段的静态同步方法

java - JAXB 解码问题