英文:
Jackson XML parsing, two wrappers have the same child element
问题
My XML是从Jira导出的工作流程。它看起来像这样:
<workflow>
<initial-actions>
<action>...</action>
<action>...</action>
</initial-actions>
<global-actions>
<action>...</action>
<action>...</action>
</global-actions>
</workflow>
尝试将其解析为POJO时,我有一个类Workflow,定义如下:
@JacksonXmlRootElement(localName = "workflow")
public class Workflow implements XMLDocumentHeader {
@JacksonXmlElementWrapper(localName = "initial-actions")
@JacksonXmlProperty(localName = "action")
private List<Action> initialActions = new ArrayList<>();
@JacksonXmlElementWrapper(localName = "global-actions")
@JacksonXmlProperty(localName = "action")
private List<Action> globalActions = new ArrayList<>();
}
但是这不被允许,因为我将localName "action"与两个setter关联起来:
java.lang.IllegalArgumentException: Conflicting setter definitions for property "action"
但是如果我省略 @JacksonXmlProperty(localName = "action")
,那么当我序列化POJO时,输出将不正确:
<initial-actions>
<initialActions>
</initialActions>
</initial-actions>
<global-actions>
<globalActions>
</globalActions>
</global-actions>
我也为Action类使用了 @JacksonXMLRootElement(localName = "action")
,但它不起作用。
如何在两个 @JacksonXmlElementWrapper
中使用相同的 @JacksonXMLProperty(localName)
?
英文:
My XML is a workflow exported from Jira. It looks like this:
<workflow>
<initial-actions>
<action>...</action>
<action>...</action>
</initial-actions>
<global-actions>
<action>...</action>
<action>...</action>
</global-actions>
</workflow>
Trying to parse it into a POJO, I have Workflow class defined like this:
@JacksonXmlRootElement(localName = "workflow")
public class Workflow implements XMLDocumentHeader {
@JacksonXmlElementWrapper(localName = "initial-actions")
@JacksonXmlProperty(localName = "action")
private List<Action> initialActions = new ArrayList<>();
@JacksonXmlElementWrapper(localName = "global-actions")
@JacksonXmlProperty(localName = "action")
private List<Action> globalActions = new ArrayList<>();
}
But that is not allowed because I have the localName "action" associated with two setters:
java.lang.IllegalArgumentException: Conflicting setter definitions for property "action"
But if I omit @JacksonXmlProperty(localName = "action")
, then when I serialize the POJO, the output will be incorrect:
<initial-actions>
<initialActions>
</initialActions>
</initial-actions>
<global-actions>
<globalActions>
</globalActions>
</global-actions>
I have @JacksonXMLRootElement(localName = "action")
for the Action class too, but it doesn't do anything.
How can I have two @JacksonXmlElementWrapper
with the same @JacksonXMLProperty(localName)
?
答案1
得分: 0
Problem solved by adding Actions class.
@JacksonXmlRootElement
public class Actions {
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "action")
private List<Action> actions = new ArrayList<>();
}
Then use Actions
instead of List<Action>
in Workflow:
@JacksonXmlProperty(localName = "initial-actions")
private Actions initialActions;
@JacksonXmlProperty(localName = "global-actions")
private Actions globalActions;
英文:
Problem solved by adding Actions class.
@JacksonXmlRootElement
public class Actions {
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "action")
private List<Action> actions = new ArrayList<>();
}
Then use Actions
instead of List<Action>
in Workflow:
@JacksonXmlProperty(localName = "initial-actions")
private Actions initialActions;
@JacksonXmlProperty(localName = "global-actions")
private Actions globalActions;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论