mule - DataWeave 2 中的空检查

标签 mule dataweave

我的网址中有一个婴儿车,并在 DW 中使用为:

<set-variable value=" #[output application/java --- ( (attributes.queryParams.filePath startsWith  ('/opt/mypath/')))] .../>

如果我不传递任何参数,我会收到异常:

  Message               : "You called the function 'startsWith' with these arguments: 
       1: Null (null)
       2: String ("/opt/mypath/")

     But it expects arguments of these types:
       1: String
       2: String 
 1| output application/java --- ( (attributes.queryParams.filePath startsWith  ('/opt/mypath/')))

1) 如何验证 null?

2)如果没有传递任何参数,那么我想返回给用户以提供参数并避免进一步的流程。可以在DataWeave中返回吗?或者我需要使用标准 Groovy 脚本来返回?

最佳答案

您可以使用条件逻辑来防止空输入,如下所示:

if (attributes.queryParams.filePath != null) (attributes.queryParams.filePath startsWith '/opt/mypath/') else null

如果您还需要防止出现空字符串,我会使用 isEmpty:

if (isEmpty(attributes.queryParams.filePath)) null else (attributes.queryParams.filePath startsWith '/opt/mypath/')

但是,如果您事先验证 attributes.queryParams.filePath 不为 null,则 DataWeave 代码会更简单。这就是整个事情的经过。如果查询参数为空,这将停止整个流程并返回错误:

<flow name="flow" doc:id="d891a730-794d-4ca5-b6d9-dd00238d89ba" >
  <http:listener doc:name="Listener" doc:id="a1cadeb8-d589-436c-8342-c13e065e4618" config-ref="HTTP_Listener_config" path="/test"/>
  <validation:is-not-null doc:name="Is not null" doc:id="66577b65-ba8d-4954-b3e7-e598310773ea" value="#[attributes.queryParams.filePath]" message='"filePath" must be included in the query parameters'/>
  <set-variable value="#[attributes.queryParams.filePath]" doc:name="Set Variable" doc:id="68511ea5-25cf-465f-ad89-c33a83ab83ec" variableName="filePath"/>
  <error-handler>
    <on-error-propagate enableNotifications="true" logException="true" doc:name="On Error Propagate" doc:id="ac12ae39-c388-4204-b1b1-b8c42aefcd66" >
    <ee:transform doc:name="Transform Message" doc:id="da695971-3ca4-435e-a4cd-b19fdd431bd6" >
      <ee:message >
        <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
error.description
]]></ee:set-payload>
        </ee:message>
      </ee:transform>
    </on-error-propagate>
  </error-handler>
</flow>

关于mule - DataWeave 2 中的空检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52212267/

相关文章:

soapui - Mule 服务代理,COULD_NOT_READ_XML_STREAM

database - 如何检查 mule 的选择组件中的有效负载是否为空?

java - 使用 JAVA (MULE) 访问有效负载

mule - 在 mule dataweave 中生成随机数

mule - DataWeave 中的动态键值对

java - Mule:SMTP 端点完整 mailto:而不是 to,主题属性

json - 将 JSON 对象作为有效负载发送时,无法在 Mule 中执行 HTTP POST

csv - 在 DataWeave 2.0 中读取不带 header 的 CSV

mule - 如何在 mule 4 中将 json 转换为 Object

http - Mule 4 在 Dataweave 中设置 HTTP 状态