JaxB2Marshaller未将XML绑定到Kotlin数据类

时间:2019-05-03 20:40:02

标签: java xml kotlin jaxb spring-batch

我正在编写一个批处理作业以解析XML,提取字段并将其保存在Databse中。解析XML时,它会拾取2个根元素,但所有字段都保留为空,因此在我的数据库中,我有2个记录将为空字段。 Cant似乎弄清楚了为什么它无法阅读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
    }

科特林数据类

@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')
}

1 个答案:

答案 0 :(得分:0)

更新:

使用Kotlin Data类时,似乎@XmlElement(name =“ *****”)并未将阅读器/编组器映射到该字段,而是使用val名称尝试映射。如果要将@XmlELement与Kotlin一起使用,则需要像这样显式创建您的吸气剂。

科特琳溶液:

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


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

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

Java解决方案:

@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;
    }