具有动态localName值的JacksonXmlRootElement [英] JacksonXmlRootElement with dynamic localName value
问题描述
我正在更新我们映射到XML
的POJO
,唯一的区别是有人既需要旧的XML
又需要新的XML
,唯一的区别是根包装器名称(所有相同的字段) ),例如目前,根Xml标签已设置为ExistingName
,并且他们希望具有所有相同字段的新值(例如BrandNewName
).而且还是变老了.有没有一种方法可以在POJO
中翻转它?
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
我知道我可以使用@JacksonXmlRootElement
设置根标签,但是可以将其设置为变量名.
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
}
推荐答案
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 ));
这篇关于具有动态localName值的JacksonXmlRootElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!