我无法使用Jackson XmlMapper反序列化包装的List。

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

I can't deserialize a wrapped List with Jackson XmlMapper

问题

I'm trying to deserialize an XML list returned from a SOAP request, but I'm getting the following error:

com.fasterxml.jackson.databind.JsonMappingException: Duplicate property 'LstMyClassInfo' for [simple type, class MyClassBLL] at [Source: (StringReader); line: 1, column: 1]

这是 XML 样本进行反序列化:

<MyClassBLL xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<LstMyClassInfo>
		<MyClassInfo>
			<SNumber>XXXXXXXXX</SNumber>
			-- 其他属性
		</MyClassInfo>
		<MyClassInfo>
			<SNumber>YYYYYYYYY</SNumber>
			-- 其他属性
		</MyClassInfo>
		<MyClassInfo>
			<SNumber>ZZZZZZZZZ</SNumber>
			-- 其他属性
		</MyClassInfo>
	</LstMyClassInfo>
	<DtIni>2023-05-01T03:00:00</DtIni>
	<DtEnd>2023-06-01T02:59:59</DtEd>
	<LstNumbers>
		<string>XXXXXXXXX</string>
		<string>YYYYYYYYY</string>
		<string>ZZZZZZZZZ</string>
	</LstNumbers>
</MyClassBLL>

我已经查看了 JacksonXml 文档,但是似乎我的实现没有问题。

英文:

I'm trying to deserialize an XML list returned from a SOAP request, but I'm getting the following error:

com.fasterxml.jackson.databind.JsonMappingException: Duplicate property 'LstMyClassInfo' for [simple type, class MyClassBLL] at [Source: (StringReader); line: 1, column: 1]

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
@JsonPropertyOrder({&quot;LstMyClassInfo&quot;, &quot;DtIni&quot;, &quot;DtEnd&quot;, &quot;LstNumbers&quot;})
public class MyClassBLL {
    @JacksonXmlElementWrapper(localName = &quot;LstMyClassInfo&quot;)
    public List&lt;MyClassInfo&gt; MyClassInfoList;
    public String DtIni;
    public String DtFDtEndnal;
    @JacksonXmlElementWrapper(localName = &quot;LstNumbers&quot;)
    public List&lt;String&gt; Numbers;
}
private MyClassBLL deserialize(String captureInformation) throws Exception {
        JacksonXmlModule xmlModule = new JacksonXmlModule();
        xmlModule.setDefaultUseWrapper(false);
        ObjectMapper objectMapper = new XmlMapper(xmlModule);

        return objectMapper.readValue(captureInformation, MyClassBLL.class);
    }

This is XML sample to deserialize:

&lt;MyClassBLL xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;
	&lt;LstMyClassInfo&gt;
		&lt;MyClassInfo&gt;
			&lt;SNumber&gt;XXXXXXXXX&lt;/SNumber&gt;
			-- Any other properties
		&lt;/MyClassInfo&gt;
		&lt;MyClassInfo&gt;
			&lt;SNumber&gt;YYYYYYYYY&lt;/SNumber&gt;
			-- Any other properties
		&lt;/MyClassInfo&gt;
		&lt;MyClassInfo&gt;
			&lt;SNumber&gt;ZZZZZZZZZ&lt;/SNumber&gt;
			-- Any other properties
		&lt;/MyClassInfo&gt;
	&lt;/LstMyClassInfo&gt;
	&lt;DtIni&gt;2023-05-01T03:00:00&lt;/DtIni&gt;
	&lt;DtEnd&gt;2023-06-01T02:59:59&lt;/DtEd&gt;
	&lt;LstNumbers&gt;
		&lt;string&gt;XXXXXXXXX&lt;/string&gt;
		&lt;string&gt;YYYYYYYYY&lt;/string&gt;
		&lt;string&gt;ZZZZZZZZZ&lt;/string&gt;
	&lt;/LstNumbers&gt;
&lt;/MyClassBLL&gt;

Anybody help-me?

PS: sorry for my bad english

I've already checked the JacksonXml documentation, but there doesn't seem to be anything wrong with my implementation.

答案1

得分: 0

你需要在MyClassBLL类的字段上添加@JacksonXmlProperty,以匹配XML元素名称。

示例

@Getter
@Setter
@ToString
@JsonPropertyOrder({ "LstMyClassInfo", "DtIni", "DtEnd", "LstNumbers" })
public class MyClassBLL {

    @JacksonXmlElementWrapper(localName = "LstMyClassInfo")
    @JacksonXmlProperty(localName="MyClassInfo")
    public List<MyClassInfo> MyClassInfoList;
    public String DtIni;
    public String DtEnd;
    @JacksonXmlElementWrapper(localName = "LstNumbers")
    @JacksonXmlProperty(localName="string")
    public List<String> Numbers;
}
英文:

You would need to add @JacksonXmlProperty to the fields in the MyClassBLL class that matches with the xml element names.

Example

@Getter
@Setter
@ToString
@JsonPropertyOrder({ &quot;LstMyClassInfo&quot;, &quot;DtIni&quot;, &quot;DtEnd&quot;, &quot;LstNumbers&quot; })
public class MyClassBLL {

    @JacksonXmlElementWrapper(localName = &quot;LstMyClassInfo&quot;)
    @JacksonXmlProperty(localName=&quot;MyClassInfo&quot;)
    public List&lt;MyClassInfo&gt; MyClassInfoList;
    public String DtIni;
    public String DtEnd;
    @JacksonXmlElementWrapper(localName = &quot;LstNumbers&quot;)
    @JacksonXmlProperty(localName=&quot;string&quot;)
    public List&lt;String&gt; Numbers;
}

huangapple
  • 本文由 发表于 2023年6月1日 01:24:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375951.html
匿名

发表评论

匿名网友

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

确定