首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用fasterxml将kotlin数据类转换为xml?

如何使用fasterxml将kotlin数据类转换为xml?
EN

Stack Overflow用户
提问于 2020-07-08 13:53:29
回答 1查看 616关注 0票数 0

我有一个要序列化为xml的kotlin数据类。我正在使用Jackson Fasterxml。然而,当我序列化这个类时,JacksonXmlProperty被忽略了,因此没有给出想要的输出。这是我正在处理的数据类。

代码语言:javascript
复制
@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:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2020-07-08 13:53:29

使用fasterxml将kotlin数据类转换为xml

  1. 确保添加对pom

依赖项

代码语言:javascript
复制
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.10.1</version>
        </dependency>

数据类上的

  1. 添加@field,以便@JacksonXmlProperty不会被忽略

代码语言:javascript
复制
@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
)

  1. 使用XmlMapper之后,您可以继续并将数据类序列化为xml

代码语言:javascript
复制
 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)

  1. Output

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62788392

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档