我从外部系统向我提供了一些XML,我需要解析它。其中一个值包含一些HTML(未编码),杰克逊遇到问题:
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
XML看起来像这样:
<lookup_info>-- [Snipped] --------------<BR/>
Some more snipped data.<BR/>
<BR/>
And some more snipped data..<BR/>
<BR/>
And even more snipped data...</lookup_info>
我的代码适用于除此之外的所有字段,我的pojo代码如下:
@JacksonXmlProperty(localName = "customer_name")
private String customerName;
@JacksonXmlProperty(localName = "email")
private String email;
@JacksonXmlProperty(localName = "lookup_info")
private String lookupInfo;
如果我将lookupInfo
更改为Object
,我可以看到杰克逊正在将原始数据映射到LinkedHashMap
...有一个条目,{BR=null}
如何强制杰克逊将原始值序列化为String
?我尝试使用@JacksonXmlText
,结果lookupInfo
为null
。
答案 0 :(得分:0)
问题是您的xml无效。如果你想在xml元素中嵌入html内容,那么你需要正确编码内容。如果要生成内容,那么任何合理的xml生成器都应该为您处理该编码。
你应该看起来像xml:<lookup_info>-- [Snipped] --------------<BR/>
Some more snipped data.<BR/>
<BR/>
And some more snipped data..<BR/>
<BR/>
And even more snipped data...</lookup_info>
答案 1 :(得分:0)
您可以尝试underscore-java库。它具有静态方法U.fromXmlMap(xml)
。我是该项目的维护者。
输出图:
{
"lookup_info": {
"#text": "-- [Snipped] --------------",
"BR": [
{
"-self-closing": "true"
},
{
"#item": {
"#text1": "\nSome more snipped data."
}
},
{
"-self-closing": "true"
},
{
"-self-closing": "true"
},
{
"#item": {
"#text2": "\nAnd some more snipped data.."
}
},
{
"-self-closing": "true"
},
{
"-self-closing": "true"
}
],
"#text3": "\nAnd even more snipped data..."
},
"#omit-xml-declaration": "yes"
}