如何在 DataWeave 2 转换脚本中使用配置参数?

问题描述

假设我有一个名为 error-messages.properties配置文件在这文件中,我有一个属性

error.invoice.missing.creationDate

当我尝试将此参数直接输入到 Dataweave 脚本中时,例如

"${error.invoice.missing.creationDate}"

我在脚本中遇到错误

Unable to resolve reference of: `$`.

当我这样做时:

"\${error.invoice.missing.creationDate"

我在脚本中没有发现任何错误,但是当我尝试运行此代码时,稍后又出现了相同的错误

import fail from dw::Runtime

output application/java
ns ns0 com.app.invoice/Invoice
var isPresent = (value,message: String) -> if (value == null or isEmpty(value)) fail(message) else value
---
{
    creationDate: isPresent(payload.invoice.creationTime,"${error.invoice.missing.creationDate}")
} as Object {
    class : "domain.command.InvoiceCommand"
} ' has errors: 
    Unable to resolve reference of $. at 9 : 9" evaluating expression: "%dw 2.0

如何将我的变量正确放入 DW 脚本中?我知道 "${some.param}" 可以在 XML 文件中使用,但如何在 DW2.0 脚本中使用?

解决方法

使用 p() 函数来引用配置属性。

https://help.mulesoft.com/s/article/How-to-read-external-property-in-DataWeave