简体   繁体   English

如何在Mule中验证content type = JSON

[英]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 :- 我有一个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>

and another flow that consume the service by placing JSON request through file inbound :- 另一个流程通过文件入站放置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>

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 .. 现在要求在文件入站终结点之后检查内容类型,是否内容类型为JSON ...如果内容类型不等于JSON,则它将在日志中不显示JSON消息。

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 :- 检查内容类型的有效载荷,如果内容类型不是JSON,它将在日志中不显示JSON,因此我将日志放置在默认的选择路由器中...但是我遇到了以下异常:-

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 ... 请提出一些更好的方法...请注意,我不想使用is-json-filter因为我想控制else条件并在日志中显示消息...

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: 您仍然可以使用is-json-filter但是需要将其包装在消息过滤器中,以便可以控制“ else”路径:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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