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

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

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:

&lt;workflow&gt;
    &lt;initial-actions&gt;
        &lt;action&gt;...&lt;/action&gt;
        &lt;action&gt;...&lt;/action&gt;
    &lt;/initial-actions&gt;
    &lt;global-actions&gt;
        &lt;action&gt;...&lt;/action&gt;
        &lt;action&gt;...&lt;/action&gt;
    &lt;/global-actions&gt;
&lt;/workflow&gt;

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

@JacksonXmlRootElement(localName = &quot;workflow&quot;)
public class Workflow implements XMLDocumentHeader {

    @JacksonXmlElementWrapper(localName = &quot;initial-actions&quot;)
    @JacksonXmlProperty(localName = &quot;action&quot;)
    private List&lt;Action&gt; initialActions = new ArrayList&lt;&gt;();
    @JacksonXmlElementWrapper(localName = &quot;global-actions&quot;)
    @JacksonXmlProperty(localName = &quot;action&quot;)
    private List&lt;Action&gt; globalActions = new ArrayList&lt;&gt;();
}

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

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:

&lt;initial-actions&gt;
	&lt;initialActions&gt;			
	&lt;/initialActions&gt;
&lt;/initial-actions&gt;
&lt;global-actions&gt;
	&lt;globalActions&gt;
    &lt;/globalActions&gt;
&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.

@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 = &quot;action&quot;)
    private List&lt;Action&gt; actions = new ArrayList&lt;&gt;();
}

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

@JacksonXmlProperty(localName = &quot;initial-actions&quot;)
private Actions initialActions;
@JacksonXmlProperty(localName = &quot;global-actions&quot;)
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:

确定