Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将带有附件的json消息发送到http:outbound endpoint Mule 3.5_Java_Rest_Jersey_Mule_Attachment - Fatal编程技术网

Java 将带有附件的json消息发送到http:outbound endpoint Mule 3.5

Java 将带有附件的json消息发送到http:outbound endpoint Mule 3.5,java,rest,jersey,mule,attachment,Java,Rest,Jersey,Mule,Attachment,编辑以更新问题: <set-variable variableName="attaches" value="#[new java.util.HashMap()]" doc:name="Variable"/> <!-- note, message object inside for loop is not same message as in flow, after for loop, original message is not

编辑以更新问题:

            <set-variable variableName="attaches" value="#[new java.util.HashMap()]" doc:name="Variable"/>
            <!-- note, message object inside for loop is not same message as in flow, after for loop, original message is not changed, so 
                 we can't add attachments to actual mule flow message   inside for loop, we need to put the attachments into a hashmap, then 
                 outside the loop add the attachments to the mule Message object  -->
            <foreach collection="#[message.payload.binaryEncodedAttachmentData.entrySet()]">
                <!-- Payload is now a Map.Entry object --> 
                <logger message="key is #[payload.key]" level="INFO" doc:name="LoggerPayloadError" />
                <logger message="value is #[payload.value]" level="INFO" doc:name="LoggerPayloadError" />
                <set-attachment attachmentName="#[payload.key]" contentType="text/plain" doc:name="Attach text file" value="#[payload.value]"/>
                <expression-component doc:name="Expression">
                    <![CDATA[java.util.Set keys = message.outboundAttachments.keySet();
                                                for(Object key:keys){
                                                 flowVars.attaches.put(key, message.outboundAttachments.get(key));
                                                }]]>
                </expression-component>
           </foreach> 
           <expression-component doc:name="Expression">
               <![CDATA[message.outboundAttachments.putAll(flowVars.attaches);]]>
           </expression-component>

           <logger message="done adding attachments" level="INFO" doc:name="Done Adding Attach" />

          <!-- done adding attachments --> 
      </when>`
我想使用mule 3.5通过mule http:outbound端点发送一个附件和一些json

            <set-variable variableName="attaches" value="#[new java.util.HashMap()]" doc:name="Variable"/>
            <!-- note, message object inside for loop is not same message as in flow, after for loop, original message is not changed, so 
                 we can't add attachments to actual mule flow message   inside for loop, we need to put the attachments into a hashmap, then 
                 outside the loop add the attachments to the mule Message object  -->
            <foreach collection="#[message.payload.binaryEncodedAttachmentData.entrySet()]">
                <!-- Payload is now a Map.Entry object --> 
                <logger message="key is #[payload.key]" level="INFO" doc:name="LoggerPayloadError" />
                <logger message="value is #[payload.value]" level="INFO" doc:name="LoggerPayloadError" />
                <set-attachment attachmentName="#[payload.key]" contentType="text/plain" doc:name="Attach text file" value="#[payload.value]"/>
                <expression-component doc:name="Expression">
                    <![CDATA[java.util.Set keys = message.outboundAttachments.keySet();
                                                for(Object key:keys){
                                                 flowVars.attaches.put(key, message.outboundAttachments.get(key));
                                                }]]>
                </expression-component>
           </foreach> 
           <expression-component doc:name="Expression">
               <![CDATA[message.outboundAttachments.putAll(flowVars.attaches);]]>
           </expression-component>

           <logger message="done adding attachments" level="INFO" doc:name="Done Adding Attach" />

          <!-- done adding attachments --> 
      </when>`
更新问题:

            <set-variable variableName="attaches" value="#[new java.util.HashMap()]" doc:name="Variable"/>
            <!-- note, message object inside for loop is not same message as in flow, after for loop, original message is not changed, so 
                 we can't add attachments to actual mule flow message   inside for loop, we need to put the attachments into a hashmap, then 
                 outside the loop add the attachments to the mule Message object  -->
            <foreach collection="#[message.payload.binaryEncodedAttachmentData.entrySet()]">
                <!-- Payload is now a Map.Entry object --> 
                <logger message="key is #[payload.key]" level="INFO" doc:name="LoggerPayloadError" />
                <logger message="value is #[payload.value]" level="INFO" doc:name="LoggerPayloadError" />
                <set-attachment attachmentName="#[payload.key]" contentType="text/plain" doc:name="Attach text file" value="#[payload.value]"/>
                <expression-component doc:name="Expression">
                    <![CDATA[java.util.Set keys = message.outboundAttachments.keySet();
                                                for(Object key:keys){
                                                 flowVars.attaches.put(key, message.outboundAttachments.get(key));
                                                }]]>
                </expression-component>
           </foreach> 
           <expression-component doc:name="Expression">
               <![CDATA[message.outboundAttachments.putAll(flowVars.attaches);]]>
           </expression-component>

           <logger message="done adding attachments" level="INFO" doc:name="Done Adding Attach" />

          <!-- done adding attachments --> 
      </when>`
我正在尝试使用mule transormers,在同一个出站端点请求中,可以这样做吗

            <set-variable variableName="attaches" value="#[new java.util.HashMap()]" doc:name="Variable"/>
            <!-- note, message object inside for loop is not same message as in flow, after for loop, original message is not changed, so 
                 we can't add attachments to actual mule flow message   inside for loop, we need to put the attachments into a hashmap, then 
                 outside the loop add the attachments to the mule Message object  -->
            <foreach collection="#[message.payload.binaryEncodedAttachmentData.entrySet()]">
                <!-- Payload is now a Map.Entry object --> 
                <logger message="key is #[payload.key]" level="INFO" doc:name="LoggerPayloadError" />
                <logger message="value is #[payload.value]" level="INFO" doc:name="LoggerPayloadError" />
                <set-attachment attachmentName="#[payload.key]" contentType="text/plain" doc:name="Attach text file" value="#[payload.value]"/>
                <expression-component doc:name="Expression">
                    <![CDATA[java.util.Set keys = message.outboundAttachments.keySet();
                                                for(Object key:keys){
                                                 flowVars.attaches.put(key, message.outboundAttachments.get(key));
                                                }]]>
                </expression-component>
           </foreach> 
           <expression-component doc:name="Expression">
               <![CDATA[message.outboundAttachments.putAll(flowVars.attaches);]]>
           </expression-component>

           <logger message="done adding attachments" level="INFO" doc:name="Done Adding Attach" />

          <!-- done adding attachments --> 
      </when>`

            <set-variable variableName="attaches" value="#[new java.util.HashMap()]" doc:name="Variable"/>
            <!-- note, message object inside for loop is not same message as in flow, after for loop, original message is not changed, so 
                 we can't add attachments to actual mule flow message   inside for loop, we need to put the attachments into a hashmap, then 
                 outside the loop add the attachments to the mule Message object  -->
            <foreach collection="#[message.payload.binaryEncodedAttachmentData.entrySet()]">
                <!-- Payload is now a Map.Entry object --> 
                <logger message="key is #[payload.key]" level="INFO" doc:name="LoggerPayloadError" />
                <logger message="value is #[payload.value]" level="INFO" doc:name="LoggerPayloadError" />
                <set-attachment attachmentName="#[payload.key]" contentType="text/plain" doc:name="Attach text file" value="#[payload.value]"/>
                <expression-component doc:name="Expression">
                    <![CDATA[java.util.Set keys = message.outboundAttachments.keySet();
                                                for(Object key:keys){
                                                 flowVars.attaches.put(key, message.outboundAttachments.get(key));
                                                }]]>
                </expression-component>
           </foreach> 
           <expression-component doc:name="Expression">
               <![CDATA[message.outboundAttachments.putAll(flowVars.attaches);]]>
           </expression-component>

           <logger message="done adding attachments" level="INFO" doc:name="Done Adding Attach" />

          <!-- done adding attachments --> 
      </when>`
原始问题: 我可以这样做吗

            <set-variable variableName="attaches" value="#[new java.util.HashMap()]" doc:name="Variable"/>
            <!-- note, message object inside for loop is not same message as in flow, after for loop, original message is not changed, so 
                 we can't add attachments to actual mule flow message   inside for loop, we need to put the attachments into a hashmap, then 
                 outside the loop add the attachments to the mule Message object  -->
            <foreach collection="#[message.payload.binaryEncodedAttachmentData.entrySet()]">
                <!-- Payload is now a Map.Entry object --> 
                <logger message="key is #[payload.key]" level="INFO" doc:name="LoggerPayloadError" />
                <logger message="value is #[payload.value]" level="INFO" doc:name="LoggerPayloadError" />
                <set-attachment attachmentName="#[payload.key]" contentType="text/plain" doc:name="Attach text file" value="#[payload.value]"/>
                <expression-component doc:name="Expression">
                    <![CDATA[java.util.Set keys = message.outboundAttachments.keySet();
                                                for(Object key:keys){
                                                 flowVars.attaches.put(key, message.outboundAttachments.get(key));
                                                }]]>
                </expression-component>
           </foreach> 
           <expression-component doc:name="Expression">
               <![CDATA[message.outboundAttachments.putAll(flowVars.attaches);]]>
           </expression-component>

           <logger message="done adding attachments" level="INFO" doc:name="Done Adding Attach" />

          <!-- done adding attachments --> 
      </when>`
  • 在Java Transformer中,构建http实体:

        // build the JSON entry
    String json = "{ \"values\" : { ";
    json += "\"Submitter\" : \"Allen\", ";
    json += "\"Short Description\" : \"testing 123\", ";
    json += "\"Attachment1\" : <Base64 encoded file data String here> ;
    json += " } }";
    
    // build the multipart entity
    HttpEntity entity = MultipartEntityBuilder.create()
            .addTextBody("entry", json, ContentType.APPLICATION_JSON)
            .addBinaryBody("attach-Attachment1", new File(filename))
            .build();
    
                <set-variable variableName="attaches" value="#[new java.util.HashMap()]" doc:name="Variable"/>
                <!-- note, message object inside for loop is not same message as in flow, after for loop, original message is not changed, so 
                     we can't add attachments to actual mule flow message   inside for loop, we need to put the attachments into a hashmap, then 
                     outside the loop add the attachments to the mule Message object  -->
                <foreach collection="#[message.payload.binaryEncodedAttachmentData.entrySet()]">
                    <!-- Payload is now a Map.Entry object --> 
                    <logger message="key is #[payload.key]" level="INFO" doc:name="LoggerPayloadError" />
                    <logger message="value is #[payload.value]" level="INFO" doc:name="LoggerPayloadError" />
                    <set-attachment attachmentName="#[payload.key]" contentType="text/plain" doc:name="Attach text file" value="#[payload.value]"/>
                    <expression-component doc:name="Expression">
                        <![CDATA[java.util.Set keys = message.outboundAttachments.keySet();
                                                    for(Object key:keys){
                                                     flowVars.attaches.put(key, message.outboundAttachments.get(key));
                                                    }]]>
                    </expression-component>
               </foreach> 
               <expression-component doc:name="Expression">
                   <![CDATA[message.outboundAttachments.putAll(flowVars.attaches);]]>
               </expression-component>
    
               <logger message="done adding attachments" level="INFO" doc:name="Done Adding Attach" />
    
              <!-- done adding attachments --> 
          </when>`
    
    //构建JSON条目
    字符串json=“{\”值\“:{”;
    json+=“\”提交者“:\”艾伦“,”;
    json+=“\”简短描述\“:\”测试123\“,”;
    json+=“\”附件1\”:;
    json+=“}}”;
    //构建多部分实体
    HttpEntity=MultipartEntityBuilder.create()
    .addTextBody(“条目”、json、ContentType.APPLICATION_json)
    .addBinaryBody(“attach-Attachment1”,新文件(文件名))
    .build();
    
  • 将此HttpEntity对象从Java transformer返回到消息负载中的mule流
  • 将流中生成的mule消息传递给http:outbound端点
  • 我正在尝试构建一个mule流,该流将向一个rest服务发送一个附件,该服务期望请求包含多部分表单数据。我希望将实体传递到出站端点,以便通过mule流而不是在java transformer内发出请求

                <set-variable variableName="attaches" value="#[new java.util.HashMap()]" doc:name="Variable"/>
                <!-- note, message object inside for loop is not same message as in flow, after for loop, original message is not changed, so 
                     we can't add attachments to actual mule flow message   inside for loop, we need to put the attachments into a hashmap, then 
                     outside the loop add the attachments to the mule Message object  -->
                <foreach collection="#[message.payload.binaryEncodedAttachmentData.entrySet()]">
                    <!-- Payload is now a Map.Entry object --> 
                    <logger message="key is #[payload.key]" level="INFO" doc:name="LoggerPayloadError" />
                    <logger message="value is #[payload.value]" level="INFO" doc:name="LoggerPayloadError" />
                    <set-attachment attachmentName="#[payload.key]" contentType="text/plain" doc:name="Attach text file" value="#[payload.value]"/>
                    <expression-component doc:name="Expression">
                        <![CDATA[java.util.Set keys = message.outboundAttachments.keySet();
                                                    for(Object key:keys){
                                                     flowVars.attaches.put(key, message.outboundAttachments.get(key));
                                                    }]]>
                    </expression-component>
               </foreach> 
               <expression-component doc:name="Expression">
                   <![CDATA[message.outboundAttachments.putAll(flowVars.attaches);]]>
               </expression-component>
    
               <logger message="done adding attachments" level="INFO" doc:name="Done Adding Attach" />
    
              <!-- done adding attachments --> 
          </when>`
    
    我确实有代码可以发布在这里,作为其他人找到这篇文章的可能解决方案。我不得不回顾旧代码,因为我的雇主决定改变我们的堆栈,我们不再使用Mule。因此,作为免责声明,我记不起我的Mule流中的这段代码是否经过测试

                <set-variable variableName="attaches" value="#[new java.util.HashMap()]" doc:name="Variable"/>
                <!-- note, message object inside for loop is not same message as in flow, after for loop, original message is not changed, so 
                     we can't add attachments to actual mule flow message   inside for loop, we need to put the attachments into a hashmap, then 
                     outside the loop add the attachments to the mule Message object  -->
                <foreach collection="#[message.payload.binaryEncodedAttachmentData.entrySet()]">
                    <!-- Payload is now a Map.Entry object --> 
                    <logger message="key is #[payload.key]" level="INFO" doc:name="LoggerPayloadError" />
                    <logger message="value is #[payload.value]" level="INFO" doc:name="LoggerPayloadError" />
                    <set-attachment attachmentName="#[payload.key]" contentType="text/plain" doc:name="Attach text file" value="#[payload.value]"/>
                    <expression-component doc:name="Expression">
                        <![CDATA[java.util.Set keys = message.outboundAttachments.keySet();
                                                    for(Object key:keys){
                                                     flowVars.attaches.put(key, message.outboundAttachments.get(key));
                                                    }]]>
                    </expression-component>
               </foreach> 
               <expression-component doc:name="Expression">
                   <![CDATA[message.outboundAttachments.putAll(flowVars.attaches);]]>
               </expression-component>
    
               <logger message="done adding attachments" level="INFO" doc:name="Done Adding Attach" />
    
              <!-- done adding attachments --> 
          </when>`
    
    
    `
    
    **注意:文档表明添加附件后负载应该为空,因此此代码遵循添加附件的上述代码,调试位可能有助于其他开发人员发现此问题,也请注意上面的免责声明,此代码未经测试

                <set-variable variableName="attaches" value="#[new java.util.HashMap()]" doc:name="Variable"/>
                <!-- note, message object inside for loop is not same message as in flow, after for loop, original message is not changed, so 
                     we can't add attachments to actual mule flow message   inside for loop, we need to put the attachments into a hashmap, then 
                     outside the loop add the attachments to the mule Message object  -->
                <foreach collection="#[message.payload.binaryEncodedAttachmentData.entrySet()]">
                    <!-- Payload is now a Map.Entry object --> 
                    <logger message="key is #[payload.key]" level="INFO" doc:name="LoggerPayloadError" />
                    <logger message="value is #[payload.value]" level="INFO" doc:name="LoggerPayloadError" />
                    <set-attachment attachmentName="#[payload.key]" contentType="text/plain" doc:name="Attach text file" value="#[payload.value]"/>
                    <expression-component doc:name="Expression">
                        <![CDATA[java.util.Set keys = message.outboundAttachments.keySet();
                                                    for(Object key:keys){
                                                     flowVars.attaches.put(key, message.outboundAttachments.get(key));
                                                    }]]>
                    </expression-component>
               </foreach> 
               <expression-component doc:name="Expression">
                   <![CDATA[message.outboundAttachments.putAll(flowVars.attaches);]]>
               </expression-component>
    
               <logger message="done adding attachments" level="INFO" doc:name="Done Adding Attach" />
    
              <!-- done adding attachments --> 
          </when>`
    
                `<set-payload value="#[null]" /> 
                <!--  below code for debugging only -->
                     <set-variable variableName="dh1" value="#[message.outboundAttachments['attach-Attachment1']]"></set-variable> 
                     <set-variable variableName="dh2" value="#[message.outboundAttachments['entry']]"></set-variable><expression-component>
                     <![CDATA[
                    ByteArrayOutputStream baos = new ByteArrayOutputStream(4);
                    DataHandler dh1 = flowVars.dh1;
            dh1.writeTo(baos);
            System.out.println("<<attach-Attachment1>>");
            System.out.println(baos.toString());
                    ByteArrayOutputStream baos2 = new ByteArrayOutputStream(4);          
                    DataHandler dh2 = flowVars.dh2;
            dh2.writeTo(baos2);
            System.out.println("<<entry>>");
            System.out.println(baos2.toString());        
                    ]]></expression-component>
    
                <!--  end code for debugging only -->`
    
    `
    `
    
    您是否尝试过在出站端点之前添加带有
    组件的附件?将实体设置为主体将不起作用,因为HTTP出站端点有自己的逻辑将有效负载和附件转换为多部分请求。啊,非常感谢@afelisatti。我将尝试一下。您好……您成功了吗相同..如果是。请分享流程和细节..我现在面临着同样的情况。嗨@Pedantic,我确实有代码可以发布在这里,我不得不回顾旧代码,因为我的雇主决定改变我们的堆栈,我们不再使用Mule。因此,作为免责声明,我不记得我的Mule流程中的这段代码是否经过测试。谢谢s@Kimk,我使用了相同的方法,它的工作方式非常有魅力。感谢您的及时回复。您是否尝试过在出站端点之前添加带有
    组件的附件?将实体设置为主体将不起作用,因为HTTP出站端点有自己的逻辑将负载和附件转换为多部分请求uest.啊,非常感谢@Afelisati。我会尝试一下。嗨..你也做到了吗..如果是的话。请分享流程和细节..我现在面临着同样的情况。嗨@Pedantic,我确实有代码可以发布在这里,我不得不回顾一下旧代码,因为我的雇主决定改变我们的堆栈,我们不再使用Mule。所以,把它作为免责声明,我不记得我的mule flow中的这段代码是否经过测试。谢谢@Kimk,我使用了相同的方法,它的工作非常有魅力。谢谢你的及时回复。