如何在Mule中验证内容类型= JSON

Ani*_*ary 2 validation json mule mule-el

我有一个Mule配置,其中有2个流: - 一个流程公开REST服务: -

<flow name="restServiceFlow1" doc:name="restFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8082" doc:name="HTTP"/>
        <jersey:resources doc:name="REST">
            <component class="com.test.services.schema.maindata.v1.Impl.MainDataImpl"/>
        </jersey:resources>
 </flow>
Run Code Online (Sandbox Code Playgroud)

和另一个通过文件入站放置JSON请求来使用服务的流程: -

<flow name="restFlow2">
  <file:inbound-endpoint path="E:\backup\test" responseTimeout="10000" connector-ref="File_Global">
    <file:filename-regex-filter pattern="aa.txt" caseSensitive="false"/>
  </file:inbound-endpoint>

  <json:json-to-object-transformer returnClass="java.util.HashMap"/>

  <foreach collection="#[payload.insertDataRequest]">
    <http:outbound-endpoint exchange-pattern="request-response"
          contentType="application/json" method="GET"
          address="http://localhost:8082/getData/insert/?id=#[payload.id]&amp;name=#[payload.name]&amp;age=#[payload.age]&amp;designation=#[payload.designation]"/>
  </foreach>
</flow>
Run Code Online (Sandbox Code Playgroud)

现在要求在文件入站端点之后检查内容类型是否内容类型是JSON ...如果内容Type不等于JSON那么它将在日志中显示不是JSON消息.

我尝试了以下内容: - 我在文件入站端点后放置了一个选择路由器: -

<when evaluator="groovy" expression="payload.ContentType=='JSON'"> 
Run Code Online (Sandbox Code Playgroud)

检查内容类型有效负载,如果内容类型不是JSON,它将在日志中显示不是JSON,因此我将日志放在Default of choice路由器中...但是我得到以下异常: -

Exception stack is:
1. No such property: ContentType for class: org.mule.transport.file.ReceiverFileInputStream (groovy.lang.MissingPropertyException)
  org.codehaus.groovy.runtime.ScriptBytecodeAdapter:50 (null)
2. groovy.lang.MissingPropertyException: No such property: ContentType for class: org.mule.transport.file.ReceiverFileInputStream (javax.script.ScriptException)
  org.codehaus.groovy.jsr223.GroovyScriptEngineImpl:323 (http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/script/ScriptException.html)
Run Code Online (Sandbox Code Playgroud)

现在有没有更好的方法来检查文件入站端点后的内容类型??? 请建议一些更好的方法...请注意我不想使用,is-json-filter因为我想控制其他条件并在日志中显示消息...

Dav*_*sot 5

您仍然可以使用,is-json-filter但需要将其包装在消息过滤器中,以便您可以控制"else"路径:

<message-filter onUnaccepted="noJsonFlow" throwOnUnaccepted="false">
  <json:is-json-filter />
</message-filter>
Run Code Online (Sandbox Code Playgroud)

  • 好的......我明白了...... noJsonFlow是我定义记录器的另一个流程 (2认同)