javaxmljacksonpojoxmlmapper

JacksonXmlRootElement with dynamic localName value


I am updating a POJO that we map to XML and the only difference is that someone wants both the old XML and some new, and with the only difference being the root Wrapper name (all the same fields), e.g. currently the root Xml Tag is set to ExistingName and they want a new value like BrandNewName with all the same fields. And still get the old. Is there a way to just flip this in the POJO?

I figured I could do it with some inheritance and a base class with two implementations, but seemed overkill

I know I can set the root tag with @JacksonXmlRootElement, but is it possible to set it to a variable name.

@JacksonXmlRootElement(localName = 'ExistingName')
class MyPojo {
    String commonVar1
    String commonVar1
    String commonVar1
}

Solution

  • I ended up finding Get Jackson XMLMapper to set root element name in code and just setting using

    XmlMapper mapper = new XmlMapper();
    .writer()
    .withRootName(myFieldName)
    .writeValueAsString(myPojo ));