英文:
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({"LstMyClassInfo", "DtIni", "DtEnd", "LstNumbers"})
public class MyClassBLL {
    @JacksonXmlElementWrapper(localName = "LstMyClassInfo")
    public List<MyClassInfo> MyClassInfoList;
    public String DtIni;
    public String DtFDtEndnal;
    @JacksonXmlElementWrapper(localName = "LstNumbers")
    public List<String> 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:
<MyClassBLL xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<LstMyClassInfo>
		<MyClassInfo>
			<SNumber>XXXXXXXXX</SNumber>
			-- Any other properties
		</MyClassInfo>
		<MyClassInfo>
			<SNumber>YYYYYYYYY</SNumber>
			-- Any other properties
		</MyClassInfo>
		<MyClassInfo>
			<SNumber>ZZZZZZZZZ</SNumber>
			-- Any other properties
		</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>
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({ "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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论