JaxB2Marshaller Not Binding XML to Kotlin Data Class

EJJ :

I am writing a batch job to parse XML, extract fields and save them in a Databse. When parsing the XML, It picks up on the 2 root elements, but leaves all the fields null, so in my Database I have 2 records will null fields. Cant seem to figure out why it cant read the Elements... TIA

  @Bean
    fun xmlFileItemReader(environment: Environment): ItemReader<ConsumerInfo> {
        val xmlFileReader = StaxEventItemReader<ConsumerInfo>()
        xmlFileReader.setResource(ClassPathResource(environment.getRequiredProperty(PROPERTY_XML_SOURCE_FILE_PATH)))
        xmlFileReader.setFragmentRootElementName("ConsumerInfo")

        val customerMarshaller = Jaxb2Marshaller()
        customerMarshaller.setClassesToBeBound(ConsumerInfo::class.java)
        xmlFileReader.setUnmarshaller(customerMarshaller)


        return xmlFileReader
    }

Kotlin data Class

@XmlRootElement(name = "ConsumerInfo", namespace = *Its correct*)
@XmlAccessorType(XmlAccessType.FIELD)
data class ConsumerInfo(


        @XmlElement(name = "LastName")
        var lastName: String? = null,
        @XmlElement(name = "FirstName")
        var firstName: String? = null,
        @XmlElement(name = "GenerationCode")
        var generationCode: String? = null,
        @XmlElement(name = "Street")
        var street: String? = null,
        @XmlElement(name = "City")
        var city: String? = null,
        @XmlElement(name = "State")
)

XML

  <ConsumerInfo>
        <LastName>lastn</LastName>
        <FirstName>firstn</FirstName>
        <GenerationCode>g</GenerationCode>
        <Street>strt</Street>
        <City>cty</City>
        <State>st</State>
    </ConsumerInfo>
    <ConsumerInfo>
        <LastName>last</LastName>
        <FirstName>first</FirstName>
        <GenerationCode>gc</GenerationCode>
        <Street>street</Street>
        <City>city</City>
        <State>state</State>
    </ConsumerInfo>
    compile('org.springframework.boot:spring-boot-starter-batch')
    compile('org.springframework.boot:spring-boot-starter-jdbc')
    compile('com.fasterxml.jackson.module:jackson-module-kotlin')
    // https://mvnrepository.com/artifact/org.springframework/spring-oxm
    compile group: 'org.springframework', name: 'spring-oxm', version: '5.1.6.RELEASE'
    compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    compile("org.jetbrains.kotlin:kotlin-reflect")
    compile ('org.postgresql:postgresql')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}
EJJ :

Update:

It appears as if @XmlElement(name = "*****") was not mapping the reader/marshaller to that field when using a Kotlin Data class, it was using the val name to try and map. If you want to use @XmlELement with Kotlin, you need to explicitly create your getters as such.

Kotlin Solution:

@XmlRootElement(name = "ConsumerInfo")
class DemoCustomer {


    @get:XmlElement(name = "FirstName")
    var firstName: String? = null

    @get:XmlElement(name = "LastName")
    var lastName: String? = null
}

Java Solution:

@XmlRootElement(name = "ConsumerInfo")
public class DemoCustomer {


    private String firstName;

    private String lastName;


    @XmlElement(name= "FirstName")
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @XmlElement(name = "LastName")
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=104723&siteId=1