我有一个要序列化为xml的kotlin数据类。我正在使用Jackson Fasterxml。然而,当我序列化这个类时,JacksonXmlProperty被忽略了,因此没有给出想要的输出。这是我正在处理的数据类。
@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
)我得到的响应忽略了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>发布于 2020-07-08 13:53:29
使用fasterxml将kotlin数据类转换为xml
依赖项
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.10.1</version>
</dependency>数据类上的
@field,以便@JacksonXmlProperty不会被忽略@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
)XmlMapper之后,您可以继续并将数据类序列化为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)<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>https://stackoverflow.com/questions/62788392
复制相似问题