简体   繁体   中英

How to validate content type=JSON in Mule

I have a Mule config in which there is 2 flows :- One flow expose a REST Service :-

<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>

and another flow that consume the service by placing JSON request through file inbound :-

<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>

Now requirement to to check the content type after the file inbound endpoint whether the content type is JSON ... if the content Type is not equal to JSON then it will show not JSON message in log ..

I have tried the following :- I placed a choice router after File inbound endpoint :-

<when evaluator="groovy" expression="payload.ContentType=='JSON'"> 

to check the content type the payload and if the content type is not JSON it will show not JSON in log and so I placed the log in Default of choice router ... But I am getting following exception :-

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)

Now is there any better way to check the content type after file inbound endpoint ??? please suggest some better way ...Please note I don't want to use is-json-filter because I want to control the else condition and display message in log ...

You can still use the is-json-filter but you need to wrap it in a message filter so you can control the "else" path:

<message-filter onUnaccepted="noJsonFlow" throwOnUnaccepted="false">
  <json:is-json-filter />
</message-filter>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM