如何将JSON响应分解为特定批处理大小的较小JSON部分

时间:2014-12-30 18:47:18

标签: json mule mule-studio

我有一个小问题......我正在公开一个REST服务,它以JSON格式获取数据列表。 以下是我的流程: -

<flow name="MainService" doc:name="MainService">
 <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>

现在,如果我点击服务,我将收到以下格式的回复: -

{
   "retrieveAllData":
   [
       {
           "response": "The Data retrieved from the Database",
           "id": 1231,
           "name": "abc",
           "age": 561,
           "designation": "Senior Software Engineer"
       },
       {
           "response": "The Data retrieved from the Database",
           "id": 165,
           "name": "test",
           "age": 561,
           "designation": "Senior Software Engineer"
       },
       {
           "response": "The Data retrieved from the Database",
           "id": 134,
           "name": "test2",
           "age": 561,
           "designation": "Senior Software Engineer"
       },
       {
           "response": "The Data retrieved from the Database",
           "id": 7,
           "name": "test3",
           "age": 561,
           "designation": "Senior Software Engineer"
       },
       {
           "response": "The Data retrieved from the Database",
           "id": 9,
           "name": "testagain",
           "age": 561,
           "designation": "Senior Software Engineer"
       },
       {
           "response": "The Data retrieved from the Database",
           "id": 10,
           "name": "newteat",
           "age": 561,
           "designation": "Senior Software Engineer"
       },
       {
           "response": "The Data retrieved from the Database",
           "id": 11,
           "name": "ani",
           "age": 561,
           "designation": "Senior Software Engineer"
       },
       {
           "response": "The Data retrieved from the Database",
           "id": 12,
           "name": "test",
           "age": 561,
           "designation": "Senior Software Engineer"
       },
       {
           "response": "The Data retrieved from the Database",
           "id": 135,
           "name": "fff",
           "age": 561,
           "designation": "Senior Software Engineer"
       },
       {
           "response": "The Data retrieved from the Database",
           "id": 166,
           "name": "fff",
           "age": 561,
           "designation": "Senior Software Engineer"
       },
       {
           "response": "The Data retrieved from the Database",
           "id": 167,
           "name": "fff",
           "age": 561,
           "designation": "Senior Software Engineer"
       },
       {
           "response": "The Data retrieved from the Database",
           "id": 444,
           "name": "fff",
           "age": 561,
           "designation": "Senior Software Engineer"
       },
       {
           "response": "The Data retrieved from the Database",
           "id": 446,
           "name": "fff",
           "age": 561,
           "designation": "Senior Software Engineer"
       },
       {
           "response": "The Data retrieved from the Database",
           "id": 447,
           "name": "fff",
           "age": 561,
           "designation": "Senior Software Engineer"
       },
       {
           "response": "The Data retrieved from the Database",
           "id": 442,
           "name": "fff",
           "age": 561,
           "designation": "Senior Software Engineer"
       },
       {
           "response": "The Data retrieved from the Database",
           "id": 133,
           "name": "fff",
           "age": 561,
           "designation": "Senior Software Engineer"
       },
       {
           "response": "The Data retrieved from the Database",
           "id": 4,
           "name": "againtest",
           "age": 561,
           "designation": "Senior Software Engineer"
       },
       {
           "response": "The Data retrieved from the Database",
           "id": 3,
           "name": "fdf",
           "age": 561,
           "designation": "Senior Software Engineer"
       },
       {
           "response": "The Data retrieved from the Database",
           "id": 8,
           "name": "fff",
           "age": 561,
           "designation": "Senior Software Engineer"
       },
       {
           "response": "The Data retrieved from the Database",
           "id": 445,
           "name": "fff",
           "age": 561,
           "designation": "Senior Software Engineer"
       }
   ]
}

现在,由于这个响应很长而且很大,我想将这个响应分成5个小部分中的较小部分: -

 {
               "response": "The Data retrieved from the Database",
               "id": 447,
               "name": "test",
               "age": 561,
               "designation": "Senior Software Engineer"
           },
           {
               "response": "The Data retrieved from the Database",
               "id": 442,
               "name": "test2",
               "age": 561,
               "designation": "Senior Software Engineer"
           },
           {
               "response": "The Data retrieved from the Database",
               "id": 133,
               "name": "fff",
               "age": 561,
               "designation": "Senior Software Engineer"
           },
           {
               "response": "The Data retrieved from the Database",
               "id": 4,
               "name": "test3",
               "age": 561,
               "designation": "Senior Software Engineer"
           },
           {
               "response": "The Data retrieved from the Database",
               "id": 3,
               "name": "fff",
               "age": 561,
               "designation": "Senior Software Engineer"
           }

所以,我遵循以下内容: - Mule - split a big JSON list into multiple smaller JSON lists

在我的流程中的泽西组件后,我提出以下内容: -

 <json:json-to-object-transformer returnClass="java.util.Map"/>
  <foreach batchSize="5">
  <json:object-to-json-transformer/>
   <logger message="ResponseLogdadasd :- #[message.payload]" level="INFO" doc:name="ResponseLogger"/>
</foreach>

现在,我遇到以下异常: -

********************************************************************************
Message               : Infinite recursion (StackOverflowError) (through reference chain: ->org.mule.DefaultMuleContext["workManager"]->org.mule.work.MuleWorkManager["muleContext"]->org.mule.DefaultMuleContext["workManager"]->org.mule.work.MuleWorkManager["muleContext"]->org.mule.DefaultMuleContext["workManager"]->org.mule.work.MuleWorkManager["muleContext"]->org.mule.DefaultMuleContext["workManager"]->org.mule.work.MuleWorkManager["muleContext"]->org.mule.DefaultMuleContext["workManager"]->org.mule.work.MuleWorkManager["muleContext"]->org.mule.DefaultMuleContext["workManager"]->org.mule.work.MuleWorkManager["muleContext"]->org.mule.DefaultMuleContext["workManager"]->org.mule.work.MuleWorkManager["muleContext"]->org.mule.DefaultMuleContext["workManager"]->org.mule.work.MuleWorkManager["muleContext"]->org.mule.DefaultMuleContext["workManager"]->org.mule.work.MuleWorkManager["muleContext"]->org.mule.DefaultMuleContext["workManager"]->org.mule.work.MuleWorkManager["muleContext"]->org.mule.DefaultMuleContext["workManager"]->org.mule.work.MuleWorkManager["muleContext"]->org.mule.DefaultMuleContext["workManager"]->org.mule.work.MuleWorkManager["muleContex...--------------------------------------------------------------------------------
Root Exception stack trace:
java.lang.StackOverflowError
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************

我做得对吗?这样我可以将响应分解为批量大小为5的较小部分吗?你的建议请

2 个答案:

答案 0 :(得分:0)

如您所提及的link所示,有效负载必须是一个集合。 java.util.Map类is not a collection

答案 1 :(得分:0)

基于您问题中的JSON,代码

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

生成一个带有一个键的地图&#34; retrieveAllData &#34; (它将包含您正在寻找的集合。)

尝试以下方法:

<foreach batchSize="5" collection="#[payload['retrieveAllData']]">
  <json:object-to-json-transformer/>
   <logger message="ResponseLogdadasd :- #[message.payload]" level="INFO" doc:name="ResponseLogger"/>
</foreach>