I found cool Jackson XML library:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.8.10</version>
<type>bundle</type>
</dependency>
It works with no extra configurations:
val xmlMapper = XmlMapper()
xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);
xmlMapper.writeValueAsString(CustomIntegration)
where CustomIntegration
is a data class
data class CustomIntegration(val name: String)
For an input CustomeIntegration("Integration A")
, an output will be
<CustomIntegration>
<name>
Integrartion A
</name>
</CustomIntegration>
The question is how I can change CustomIntegration
to integration
when deserializing to XML? There are numbers of use-cases I want to address:
CoolIntegration
, AwesomeIntegration
all will convert into tag integration
CustomIntegration
into integration
because it is almost like SOAP:envelope
into SOAP integrations, so in the same tag can be with different contentThe only solution I found so far is to write custom deserializers but this wouldn't fully address all non-functional requirements I am trying to meet.
The solution I found in another SO post.
In order to change the root element name in XML, you need to put @JsonRootName(value = "integration")
annotation.
@JsonRootName(value = "integration")
data class CustomIntegration(val name: String)
Output will be:
<integration>
<name>
Integrartion A
</name>
</integration>