如果查询参数说" requestId "我需要抛出异常请求URL中不存在。
网址如下所示:my.server.com?requestId=123&age=26
我在消息属性中记录 requestId ,如下所示:
<message-properties-transformer scope="session" doc:name="Adding requestId">
<add-message-property key="requestId" value="#[message.inboundProperties.'http.query.params'.requestId]"/>
</message-properties-transformer>
如果URL不包含requestId,则它将为null。但在这种情况下,我想检查 requestId 是否存在。 然后
答案 0 :(得分:2)
As per your requirement you can do the following :-
<choice doc:name="Choice">
<when expression="#[org.mule.util.StringUtils.isNotEmpty(message.inboundProperties.'http.query.params'.requestId)]">
<logger message="query parameter exist" level="INFO" doc:name="Logger"/>
</when>
<otherwise>
<logger message="query parameter does not exist" level="INFO" doc:name="Logger"/>
<!-- Throw your exception here -->
<scripting:component doc:name="Script">
<scripting:script engine="Groovy"><![CDATA[
throw new IllegalArgumentException('query parameter does not exist')
]]></scripting:script>
</scripting:component>
</otherwise>
</choice>
Here If Query parameter exists it will simply log in a logger and if Query parameter does not exists it will throw the exception you want ..
答案 1 :(得分:2)
感谢大家的想法。最后,我使用了一个简单的方法,使用groovy,我不需要放任何选择路由器或任何表达式过滤器。他是我的代码:
if(!message.getInboundProperty("http.query.params").find{ it.key == "requestId" }){
throw new IllegalArgumentException('requestId does not exist');
}
答案 2 :(得分:1)
您是否尝试过邮件过滤器?
<message-filter throwOnUnaccepted="true">
<expression-filter
expression="#[message.inboundProperties.'http.query.params'.requestId != empty]" />
</message-filter>
当requestId为null,false,empty,zero或空集合时,这将抛出FilterUnacceptedException。
如果过滤器需要接受零,则必须更改条件。
答案 3 :(得分:0)
Use the below expression to check the query parameter availability in HTTP URL
#[org.mule.util.StringUtils.isNotEmpty(message.inboundProperties.'http.query.params'.requestId)]
See below sample code that used choice router and based on query param availability it routes the control
<choice doc:name="Choice">
<when expression="#[org.mule.util.StringUtils.isNotEmpty(message.inboundProperties.'http.query.params'.requestId)]">
<logger message="Query param available in request level="INFO" doc:name="Logger"/>
</when>
<otherwise>
<logger message="Query param not available in request level="INFO" doc:name="Logger"/>
</otherwise>
</choice>