How do I convert kotlin data class to xml using fasterxml?
I have a kotlin data class that I would like to serialize to xml. I am using Jackson Fasterxml. However, when I serialize the class, the JacksonXmlProperty is ignored thus not giving the desired output. Here’s the data class I was working on.
@JacksonXmlRootElement(localName = "COMMAND") data class Request( @JacksonXmlProperty(localName = "TYPE") val type: String, @JacksonXmlProperty(localName = "INTERFACEID") val interfaceId: String, @JacksonXmlProperty(localName = "MSISDN") val msisdn: String, @JacksonXmlProperty(localName = "MSISDN2") val msisdn2: String, @JacksonXmlProperty(localName = "AMOUNT") val amount: Int, @JacksonXmlProperty(localName = "MEMO") val memo: String, @JacksonXmlProperty(localName = "EXTTRID") val externalTxnId: String, @JacksonXmlProperty(localName = "MERCHANT_TXN_ID") val merchantTxnId: String, @JacksonXmlProperty(localName = "IS_TRANS_UNIQUE_CHECK_REQUIRED") val isUnique: String = "Y", @JacksonXmlProperty(localName = "REFERENCE") val reference: String, @JacksonXmlProperty(localName = "serviceType") val serviceType: String, @JacksonXmlProperty(localName = "USERNAME") val username: String, @JacksonXmlProperty(localName = "PASSWORD") val password: String )
The response I was getting ignores the JacksonXmlProperty:
<COMMAND> <serviceType>MERCHPAY</serviceType> <type>MERCHPAY</type> <interfaceId>DATABUNDLES</interfaceId> <msisdn>733204938</msisdn> <msisdn2>100001929</msisdn2> <amount>1000</amount> <memo>Enter the PIN for payment of 1000 to purchase testing transaction</memo> <externalTxnId>07026984141550752666</externalTxnId> <merchantTxnId>07026984141550752666</merchantTxnId> <reference>Testing transaction</reference> <username>abcd</username> <password>abcd123</password> </COMMAND>
To convert kotlin data class to xml using fasterxml
- Ensure you add dependency on pom
<dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.10.1</version> </dependency>
- On the data class add
@field
so that the@JacksonXmlProperty
is not ignored
@JacksonXmlRootElement(localName = "COMMAND") data class AirtelExpressRequest( @field:JacksonXmlProperty(localName = "TYPE") val type: String, @field:JacksonXmlProperty(localName = "INTERFACEID") val interfaceId: String, @field:JacksonXmlProperty(localName = "MSISDN") val msisdn: String, @field:JacksonXmlProperty(localName = "MSISDN2") val msisdn2: String, @field:JacksonXmlProperty(localName = "AMOUNT") val amount: Int, @field:JacksonXmlProperty(localName = "MEMO") val memo: String, @field:JacksonXmlProperty(localName = "EXTTRID") val externalTxnId: String, @field:JacksonXmlProperty(localName = "MERCHANT_TXN_ID") val merchantTxnId: String, @field:JacksonXmlProperty(localName = "IS_TRANS_UNIQUE_CHECK_REQUIRED") val isUnique: String = "Y", @field:JacksonXmlProperty(localName = "REFERENCE") val reference: String, @field:JacksonXmlProperty(localName = "serviceType") val serviceType: String, @field:JacksonXmlProperty(localName = "USERNAME") val username: String, @field:JacksonXmlProperty(localName = "PASSWORD") val password: String )
- Using
XmlMapper
you can then go ahead and serialize to the data class to xml
val xmlMapper = XmlMapper( JacksonXmlModule().apply { setDefaultUseWrapper(false) } ).apply { enable(SerializationFeature.INDENT_OUTPUT) } val strObject = Request( type = "MERCHPAY", interfaceId = "DATABUNDLES", msisdn = "733204938", msisdn2 = "100001929", amount = 1_000, externalTxnId = "07026984141550752666", merchantTxnId = "07026984141550752666", reference = "Testing transaction", memo = "Enter the PIN for payment of 1000 to purchase testing transaction", serviceType = "MERCHPAY", username = "abcd", password = "abcd123" ) val xml = xmlMapper.writeValueAsString(strObject)
- Output
<COMMAND> <TYPE>MERCHPAY</TYPE> <INTERFACEID>DATABUNDLES</INTERFACEID> <MSISDN>733204938</MSISDN> <MSISDN2>100001929</MSISDN2> <AMOUNT>1000</AMOUNT> <MEMO>Enter the PIN for payment of 1000 to purchase testing transaction</MEMO> <EXTTRID>07026984141550752666</EXTTRID> <MERCHANT_TXN_ID>07026984141550752666</MERCHANT_TXN_ID> <IS_TRANS_UNIQUE_CHECK_REQUIRED>Y</IS_TRANS_UNIQUE_CHECK_REQUIRED> <REFERENCE>Testing transaction</REFERENCE> <serviceType>MERCHPAY</serviceType> <USERNAME>abcd</USERNAME> <PASSWORD>abcd123</PASSWORD> </COMMAND>