Search code examples
javajacksonpojojackson-dataformat-xml

Jackson XML Parser : Getting Null data after deserialization


I am trying deserialize the xml data using Jackson. I created the Pojo classes but after mapping when i try to print the data, I am getting Null value.

Below are the details -

XML Data

<?xml version="1.0" encoding="UTF-8"?>
<VTX_SHS reporting_date="20210228" interface_type="buss" event_type="green_light" >
  <!-- 01:  period                                   |         varchar2(10) -->
  <!-- 02:  book_base_ent_cd                         |         varchar2(10) -->
  <!-- 03:  isin                                     |         varchar2(12) -->
 
  <row period="2021-02-28" book_base_ent_cd="U0027" other_inst_ident="PLCHS258Q463" />
  <row period="2021-02-28" book_base_ent_cd="U0028" other_inst_ident="PLCHS258Q464" />
  <row period="2021-02-28" book_base_ent_cd="U0029" other_inst_ident="PLCHS258Q465" />
 
</VTX_SHS>

POJO Classes

VTX_SHS.java

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;

import java.util.List;
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class VTX_SHS implements XmlData{
        public List<row> row;
        public int reporting_date;
        public String interface_type;
        public String event_type;
}

row.java

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;

import java.util.Date;
import java.util.List;

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class row implements XmlData {
        public Date period;
        public String book_base_ent_cd;
        public String other_inst_ident;
}

XmlData.java

import java.io.Serializable;

public interface XmlData extends Serializable {
}

SHSCompare.java

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import lombok.var;
import models.VTX_SHS;

import java.io.File;
import java.io.IOException;

public class SHSCompare {
    public static void main(String[] args) throws IOException {
        XmlMapper xmlMapper = new XmlMapper();
        File file = new File("src/test/resources/Files/data.xml");
        var SHS1Response = xmlMapper.readValue(file, VTX_SHS.class);
        System.out.println(SHS1Response);
    }
}

In my output I am getting null values for all the fields in row tag.

VTX_SHS(row=[], reporting_date=20210228, interface_type=buss, event_type=green_light, 

Can anyone suggest what have I missed here?

Best Regards


Solution

  • You need to use the proper annotation: JacksonXmlElementWrapper

    http://fasterxml.github.io/jackson-dataformat-xml/javadoc/2.2.0/com/fasterxml/jackson/dataformat/xml/annotation/JacksonXmlElementWrapper.html

    Example:

    @Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    static class VTX_SHS implements Serializable {
    
        @JacksonXmlElementWrapper(useWrapping = false)
        public List<Row> row;
    
        public int reporting_date;
        public String interface_type;
        public String event_type;
    }
    
    @Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    static class Row implements Serializable {
        public Date period;
        public String book_base_ent_cd;
        public String other_inst_ident;
    }
    
    public static void main(String[] args) throws JsonProcessingException {
    
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                + "<VTX_SHS reporting_date=\"20210228\" interface_type=\"buss\" event_type=\"green_light\" >\n"
                + "  <!-- 01:  period                                   |         varchar2(10) -->\n"
                + "  <!-- 02:  book_base_ent_cd                         |         varchar2(10) -->\n"
                + "  <!-- 03:  isin                                     |         varchar2(12) -->\n"
                + " \n"
                + "  <row period=\"2021-02-28\" book_base_ent_cd=\"U0027\" other_inst_ident=\"PLCHS258Q463\" />\n"
                + "  <row period=\"2021-02-28\" book_base_ent_cd=\"U0028\" other_inst_ident=\"PLCHS258Q464\" />\n"
                + "  <row period=\"2021-02-28\" book_base_ent_cd=\"U0029\" other_inst_ident=\"PLCHS258Q465\" />\n"
                + " \n"
                + "</VTX_SHS>";
    
        XmlMapper xmlMapper = new XmlMapper();
        var SHS1Response = xmlMapper.readValue(xml, VTX_SHS.class);
        System.out.println(SHS1Response);
    
    }
    

    Console:

    VTX_SHS(row=[Row(period=Sun Feb 28 01:00:00 CET 2021, book_base_ent_cd=U0027, other_inst_ident=PLCHS258Q463), Row(period=Sun Feb 28 01:00:00 CET 2021, book_base_ent_cd=U0028, other_inst_ident=PLCHS258Q464), Row(period=Sun Feb 28 01:00:00 CET 2021, book_base_ent_cd=U0029, other_inst_ident=PLCHS258Q465)], reporting_date=20210228, interface_type=buss, event_type=green_light)
    
    Process finished with exit code 0
    

    1. Static inner classes used for example purpose only.
    2. Please use LocalDate, Date is obsolete