1.关于boolean的配置
freemarker中输出时可以使用这种方式输出${xxx?string("true","flase")}
当xxx为true时显示字符串true,否则为字符串false,当然true,false字符串也可以换成其他字符串,比如yes和no。
单纯的输出true/false,可以这样写 ${xxx?c}
"isBundle": "${APIProfileMaintenanceService.response.body.isBundle?c}"
2.关于对象为空的配置
有时候请求或者响应里,如果有一个对象会为空,要对对象进行null的判断,不然会抛异常。
如下所示
需要用<#if ()??> </#if> 括号内填写条件
{
"body":
{
"productOfferingPrice":
{
<#if (APIOfferMaintenanceService.response.body.productOfferingPrice)??>
"unitOfMeasure":"${APIOfferMaintenanceService.response.body.productOfferingPrice.unitOfMeasure!}"
</#if >
}
}
}
3.For "." left-hand operand: Expected a hash, but this has evaluated to a sequence (wrapper: f.t.SimpleSequence):
==> APIBillingMaintenanceService.response.body.transcationList [in template "sid-ED4B5F2B-1A2F-4A0F-80E4-8EEB0DD65CA9" at line 1, column 2308]
这个错误一般是模板内部写的有问题,比如我这个,
{
"body":
{
"transcationList":
[
<#if (APIBillingMaintenanceService.response.body.transcationList)??>
<#list APIBillingMaintenanceService.response.body.transcationList as transcationList>
{
"operationCharacteristic":
[
<#if (APIBillingMaintenanceService.response.body.transcationList.operationCharacteristic)??>
<#list APIBillingMaintenanceService.response.body.transcationList.operationCharacteristic as operationCharacteristic>
{
"name":"${operationCharacteristic.name!}",
"value":"${operationCharacteristic.value!}"
}
</#list>
</#if>
]
</#list>
</#if>
]
}
}
外部list起了别名,内部List就不能从最开始的APIBillingMaintenanceService.response.body.transcationList开始,更改如下
{
"body":
{
"transcationList":
[
<#if (APIBillingMaintenanceService.response.body.transcationList)??>
<#list APIBillingMaintenanceService.response.body.transcationList as transcationList>
{
"operationCharacteristic":
[
<#if (transcationList.operationCharacteristic)??>
<#list transcationList.operationCharacteristic as operationCharacteristic>
{
"name":"${operationCharacteristic.name!}",
"value":"${operationCharacteristic.value!}"
}
</#list>
</#if>
]
</#list>
</#if>
]
}
}
4.freemarker处理List,process之后为空
{"BillingAccountList":[]}
如上,这是freemarker处理之后显示的内容,list里的所有东西都丢了,模板如下
{
"body":
{
"BillingAccountList":
[
<#if (APIProfileQueryService.response.body.BillingAccountList)??>
<#list APIProfileQueryService.response.body.BillingAccountList as BillingAccountList>
{
"defaultPaymentMethod":
{
"id":"${BillingAccountList.defaultPaymentMethod.id!}",
"type":"${BillingAccountList.defaultPaymentMethod.type!}"
}
</#list>
</#if>
]
}
}
经过测试,将所有的BillingAccountList改成billingAccountList,就有映射值了
{
"body":
{
"billingAccountList":
[
<#if (APIProfileQueryService.response.body.billingAccountList)??>
<#list APIProfileQueryService.response.body.billingAccountList as billingAccountList>
{
"defaultPaymentMethod":
{
"id":"${billingAccountList.defaultPaymentMethod.id!}",
"type":"${billingAccountList.defaultPaymentMethod.type!}"
}
</#list>
</#if>
]
}
}
暂时原理未知,没有细看源码