FastXML -Jackson XML扩展将值转换为POJO中的null



我决定在我的项目中给杰克逊XML一个机会将XML字符串转换为Pojo的机会。

所以我使用以下很棒的网站从我的XML字符串生成POJO。

因此,它创建了我需要的类,然后我尝试将XML值读取到这些对象中。

不幸的是,某些值在对象中设置为null,我不知道原因:

这是XML字符串的开始:

<?xml version="1.0" encoding="UTF-8"?>
<schedule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" generated_at="2018-02-23T16:55:45+00:00" xmlns="http://schemas.thesite.com/jobsapi/v1/myjob" xsi:schemaLocation="http://schemas.thesite.com/jobsapi/v1/myjob http://schemas.thesite.com/asb/myjob/v1/xml/endpoints/myjob/schedule.xsd">
    <job_event id="sr:task:12004724" scheduled="2018-02-23T17:00:00+00:00" start_time_tbd="false" status="not_started">
        <task type="group" number="23"/>

我的课程是:

public class Schedule
{
    private String generated_at;
    private String xmlns;
    private Job_event[] job_events;
    public String getGenerated_at ()
    {
        return generated_at;
    }
    public void setGenerated_at (String generated_at)
    {
        this.generated_at = generated_at;
    }
    public String getXmlns ()
    {
        return xmlns;
    }
    public void setXmlns (String xmlns)
    {
        this.xmlns = xmlns;
    }
    public Job_event[] getjob_events ()
    {
        return job_events;
    }
    public void setjob_events (Job_event[] job_events)
    {
        this.job_events = job_events;
    }
    @Override
    public String toString()
    {
        return "Schedule [generated_at = "+generated_at+", xmlns = "+xmlns+", job_events = "+job_events+"]";
    }
}

public class Job_event {
    private String id;
    private String scheduled;
    private String start_time_tbd;
    private String status;
        //more fields, getters and setters   
}

然后,我用它将字符串转换为pojos:

ObjectMapper xmlMapper = new XmlMapper();
xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Schedule sched = xmlMapper.readValue(xmlString, Schedule.class);
Job_event[] job_events = sched.getJob_events();

我在job_events中的每个事件值中都发现成员值之类的ID和状态以及其余的为null。

为什么?XML字符串还可以。

谢谢!

您可以通过执行以下

来修复它
  1. 使用@JacksonXmlProperty(isAttribute = true)注释将POJO字段映射到XML属性
  2. 使用List代替数组
  3. 使用@JacksonXmlElementWrapper(useWrapping = false)注释来发出信号,表示您的列表List<JobEvent>表示为重复的XML元素。
  4. 我还认为您应该用localName注释您的字段,以保持与Java naming condentions兼容

之后看起来像

@JsonIgnoreProperties(ignoreUnknown = true)
public class JobEvent {
    @JacksonXmlProperty(isAttribute = true)
    private String id;
    @JacksonXmlProperty(isAttribute = true)
    private String scheduled;
    @JacksonXmlProperty(isAttribute = true, localName = "start_time_tbd")
    private String startTimeTbd;
    @JacksonXmlProperty(isAttribute = true)
    private String status;
    @Override
    public String toString() {
        return id + " " + scheduled + " " + " " + startTimeTbd + " " + status;
    }
}

schepary.java

public class Schedule {
    @JacksonXmlProperty(isAttribute = true,localName="generated_at")
    public String generatedAt;
    @JacksonXmlProperty(isAttribute = true)
    public String xmlns;
     @JacksonXmlElementWrapper(useWrapping = false)
    public List<JobEvent> job_event;
    @Override
    public String toString()
    {
        return "Schedule [generated_at = "+generatedAt+", xmlns = "+xmlns+", job_events = "+job_event.toString()+"]";
    }
}

TestCall

@Test
    public void readXmlToSelfDefinedPojo2() throws Exception {
        ObjectMapper mapper = new XmlMapper();
        Schedule pojo = mapper.readValue(
                        Thread.currentThread().getContextClassLoader().getResourceAsStream("48963966.xml"),
                        Schedule.class);
        System.out.println(pojo + "");
    }

对于XML文件48963966.xml

将打印

Schedule [generated_at = 2018-02-23T16:55:45+00:00, xmlns = null, job_events = [sr:task:12004724 2018-02-23T17:00:00+00:00  false not_started, sr:task:12004725 2018-02-25T17:00:00+00:00  false not_started]]

相关内容

  • 没有找到相关文章

最新更新