0

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:

  • Single implementation for any class, so CoolIntegration, AwesomeIntegration all will convert into tag integration
  • I cannot just rename class CustomIntegration into integration because it is almost like SOAP:envelope into SOAP integrations, so in the same tag can be with different content
  • Ideally, I want to write as little code as possible, to simplify the maintenance of it

The 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.

Dmytro Chasovskyi
  • 3,209
  • 4
  • 40
  • 82

1 Answers1

1

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>
Dmytro Chasovskyi
  • 3,209
  • 4
  • 40
  • 82