我有一個json響應,我使用Apache Valocity模板將其轉換爲xml。我的JSON的格式如下:使用Apache Velocity模板的Json映射
{
"value": {
"1234": {
"7": ["abcde"],
"2": ["abcde", "lkisy", "skjsk"],
"1": ["aksdja", "msnd", "knsda"]
},
"7654": {
"8": ["NA"],
"4": ["NA"],
"7": ["BA"]
},
"0987": {
"8": ["PO"],
"3": ["LO"],
"2": ["KI"]
}
}
}
我Velocity模板的格式如下:
<VelocityResponse xmlns="xsd location namespace">
<HeaderID>${headers.HeaderID}</HeaderID>
<FirstTag>
#foreach($body in ${body.value})
<SecondTag>
#set($keys = $body.keySet())
#foreach($key in $keys)
<ThirdTag>$key</ThirdTag>
#set($someValue = $key.keySet())
#foreach($value in $someValue)
<FourthTag>$value</FourthTag>
<FifthTag>
#foreach($userInfo in $someValue[$value])
<SixthTag>$userInfo</SixthTag>
#end
</FifthTag>
#end
</SecondTag>
#end
</FirstTag>
</VelocityResponse>
當我嘗試映射此,我收到此錯誤:
198 - org.apache.camel.camel-core - 2.15.1.redhat-621084 | Error : org.apache.velocity.exception.ParseErrorException: Encountered "<EOF>" at org.apache.camel.component.velocity.VelocityEndpoint[line 21, column 27]
n | Was expecting one of:
n | "(" ...
n | <RPAREN> ...
n | <ESCAPE_DIRECTIVE> ...
n | <SET_DIRECTIVE> ...
n | "##" ...
n | "\\\\" ...
n | "\\" ...
n | <TEXT> ...
n | "*#" ...
n | "*#" ...
n | "]]#" ...
n | <STRING_LITERAL> ...
n | <END> ...
n | <IF_DIRECTIVE> ...
n | <INTEGER_LITERAL> ...
n | <FLOATING_POINT_LITERAL> ...
n | <WORD> ...
n | <BRACKETED_WORD> ...
n | <IDENTIFIER> ...
n | <DOT> ...
n | "{" ...
n | "}" ...
n | <EMPTY_INDEX> ...
n |
得到任何想法?
請檢查速度模板中使用的所有變量是否也在速度模型中。不確定,在第一個foreach中,「$ {body.value}」是正確的。如果body在你的模型中,也嘗試使用getter在模板中訪問它,如$ body.getValue() – dumitru
嗨,實際上我正在獲取值。 –