Jackson XML解析,两个包装器具有相同的子元素。

huangapple go评论104阅读模式
英文:

Jackson XML parsing, two wrappers have the same child element

问题

My XML是从Jira导出的工作流程。它看起来像这样:

  1. <workflow>
  2. <initial-actions>
  3. <action>...</action>
  4. <action>...</action>
  5. </initial-actions>
  6. <global-actions>
  7. <action>...</action>
  8. <action>...</action>
  9. </global-actions>
  10. </workflow>

尝试将其解析为POJO时,我有一个类Workflow,定义如下:

  1. @JacksonXmlRootElement(localName = "workflow")
  2. public class Workflow implements XMLDocumentHeader {
  3. @JacksonXmlElementWrapper(localName = "initial-actions")
  4. @JacksonXmlProperty(localName = "action")
  5. private List<Action> initialActions = new ArrayList<>();
  6. @JacksonXmlElementWrapper(localName = "global-actions")
  7. @JacksonXmlProperty(localName = "action")
  8. private List<Action> globalActions = new ArrayList<>();
  9. }

但是这不被允许,因为我将localName "action"与两个setter关联起来:

  1. java.lang.IllegalArgumentException: Conflicting setter definitions for property "action"

但是如果我省略 @JacksonXmlProperty(localName = "action"),那么当我序列化POJO时,输出将不正确:

  1. <initial-actions>
  2. <initialActions>
  3. </initialActions>
  4. </initial-actions>
  5. <global-actions>
  6. <globalActions>
  7. </globalActions>
  8. </global-actions>

我也为Action类使用了 @JacksonXMLRootElement(localName = "action"),但它不起作用。

如何在两个 @JacksonXmlElementWrapper 中使用相同的 @JacksonXMLProperty(localName)

英文:

My XML is a workflow exported from Jira. It looks like this:

  1. &lt;workflow&gt;
  2. &lt;initial-actions&gt;
  3. &lt;action&gt;...&lt;/action&gt;
  4. &lt;action&gt;...&lt;/action&gt;
  5. &lt;/initial-actions&gt;
  6. &lt;global-actions&gt;
  7. &lt;action&gt;...&lt;/action&gt;
  8. &lt;action&gt;...&lt;/action&gt;
  9. &lt;/global-actions&gt;
  10. &lt;/workflow&gt;

Trying to parse it into a POJO, I have Workflow class defined like this:

  1. @JacksonXmlRootElement(localName = &quot;workflow&quot;)
  2. public class Workflow implements XMLDocumentHeader {
  3. @JacksonXmlElementWrapper(localName = &quot;initial-actions&quot;)
  4. @JacksonXmlProperty(localName = &quot;action&quot;)
  5. private List&lt;Action&gt; initialActions = new ArrayList&lt;&gt;();
  6. @JacksonXmlElementWrapper(localName = &quot;global-actions&quot;)
  7. @JacksonXmlProperty(localName = &quot;action&quot;)
  8. private List&lt;Action&gt; globalActions = new ArrayList&lt;&gt;();
  9. }

But that is not allowed because I have the localName "action" associated with two setters:

  1. java.lang.IllegalArgumentException: Conflicting setter definitions for property &quot;action&quot;

But if I omit @JacksonXmlProperty(localName = &quot;action&quot;), then when I serialize the POJO, the output will be incorrect:

  1. &lt;initial-actions&gt;
  2. &lt;initialActions&gt;
  3. &lt;/initialActions&gt;
  4. &lt;/initial-actions&gt;
  5. &lt;global-actions&gt;
  6. &lt;globalActions&gt;
  7. &lt;/globalActions&gt;
  8. &lt;/global-actions&gt;

I have @JacksonXMLRootElement(localName = &quot;action&quot;) 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.

  1. @JacksonXmlRootElement
  2. public class Actions {
  3. @JacksonXmlElementWrapper(useWrapping = false)
  4. @JacksonXmlProperty(localName = "action")
  5. private List<Action> actions = new ArrayList<>();
  6. }

Then use Actions instead of List<Action> in Workflow:

  1. @JacksonXmlProperty(localName = "initial-actions")
  2. private Actions initialActions;
  3. @JacksonXmlProperty(localName = "global-actions")
  4. private Actions globalActions;
英文:

Problem solved by adding Actions class.

  1. @JacksonXmlRootElement
  2. public class Actions {
  3. @JacksonXmlElementWrapper(useWrapping = false)
  4. @JacksonXmlProperty(localName = &quot;action&quot;)
  5. private List&lt;Action&gt; actions = new ArrayList&lt;&gt;();
  6. }

Then use Actions instead of List&lt;Action&gt; in Workflow:

  1. @JacksonXmlProperty(localName = &quot;initial-actions&quot;)
  2. private Actions initialActions;
  3. @JacksonXmlProperty(localName = &quot;global-actions&quot;)
  4. private Actions globalActions;

huangapple
  • 本文由 发表于 2023年5月25日 09:36:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定