【问题标题】:How to flush xml data in request by using HTTP connector in mule如何在 mule 中使用 HTTP 连接器刷新请求中的 xml 数据
【发布时间】:2016-06-01 19:15:46
【问题描述】:

我正在使用 java 组件发送请求,它对我来说工作正常。代码是。

 public class parseXmlToString implements Callable 
    {
        public Object onCall(MuleEventContext eventContext) throws Exception 
        {
            String xmlData = eventContext.getMessage().getInvocationProperty("sampleXmlData");
            String content = URLEncoder.encode(xmlData, "UTF-8");
            eventContext.getMessage().setInvocationProperty("xmlData", content);

            try
            {
                URL url = new URL(webServiceURL);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setUseCaches(false);
                conn.setRequestMethod("POST");
                conn.connect();

                DataOutputStream output = null;
                output = new DataOutputStream(conn.getOutputStream());

                String content = "xmlData=" + URLEncoder.encode(xmlData, "UTF-8");

                output.writeBytes(content);
                output.flush();
                output.close();

                if (conn.getResponseCode() != 200) 
                {
                    System.out.println(conn.getResponseCode());
                    throw new IOException(conn.getResponseMessage());
                }

                BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = rd.readLine()) != null) 
                {
                    sb.append(line);
                }

                rd.close();
                conn.disconnect();
                eventContext.getMessage().setPayload("Import Initiated");
            }
            catch (Exception e)
            {
                System.out.println("Failed REST service call. " + e.getMessage());
                e.printStackTrace();
            }
            eventContext.getMessage().setPayload(content);
            return eventContext.getMessage().getPayload();enter code here
        }

    }

现在我正在尝试通过在 mule 中使用 HTTP 连接器发送请求,为此我正在发送带有请求参数的数据。这个代码是

<http:request config-ref="HTTP_Request_Configuration1" path="/#flowVars.outputAppName]/services/EmployeeService/importUsers" method="POST" doc:name="HTTP" parseResponse="false">
<http:request-builder> 
<http:query-param paramName="serviceId" value="#[flowVars.outputServiceID]"/>
<http:query-param paramName="expiresOn" value="#[flowVars.expiresOnImport]"/>
<http:query-param paramName="importId" value="#[flowVars.importId]"/>
<http:query-param paramName="signature" value="#[flowVars.signatureImport]"/>
<http:header headerName="xmlData" value="#[flowVars.xmlData]"/>
</http:request-builder>
 </http:request>

如果我运行它然后我得到以下错误

    ERROR 2015-03-09 11:03:31,251 [[catalystoneconnector].HTTP_Listener_Configuration.worker.01] org.mule.exception.DefaultMessagingExceptionStrategy: 
    ********************************************************************************
    Message               : Response code 500 mapped as failure. Message payload is of type: BufferInputStream
    Code                  : MULE_ERROR--2
    --------------------------------------------------------------------------------
    Exception stack is:
    1. Response code 500 mapped as failure. Message payload is of type: BufferInputStream (org.mule.module.http.internal.request.ResponseValidatorException)
      org.mule.module.http.internal.request.SuccessStatusCodeValidator:37 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/module/http/internal/request/ResponseValidatorException.html)
    --------------------------------------------------------------------------------
    Root Exception stack trace:
    org.mule.module.http.internal.request.ResponseValidatorException: Response code 500 mapped as failure. Message payload is of type: BufferInputStream
        at org.mule.module.http.internal.request.SuccessStatusCodeValidator.validate(SuccessStatusCodeValidator.java:37)
        at org.mule.module.http.internal.request.DefaultHttpRequester.innerProcess(DefaultHttpRequester.java:202)
        at org.mule.module.http.internal.request.DefaultHttpRequester.process(DefaultHttpRequester.java:166)
        + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
    ********************************************************************************

我还尝试将 XML 数据作为查询参数发送,就像 http 连接器中的其他参数一样。它也不起作用。

我做错了什么,或者有什么正确的方法可以以 Mule 的方式做到这一点?喜欢在请求中发送一个大的 xml 吗?

【问题讨论】:

  • 为什么用http:headerheader 中传递XML 数据?你不是用output.writeBytes(content);output.writeBytes(content);在你的Java代码中的请求实体body中传递它吗?
  • 是的,我正在使用 output.writeBytes(content) 传递 XML 数据。但是如果我使用 HTTP 连接器,那么我就不会使用 java 来发送请求和数据。我想通过骡子方式发送。那么如何使用 mule HTTP 连接器发送它呢?

标签: java mule mule-studio mule-component


【解决方案1】:

您需要在 POST 请求中添加实体主体。这是来自documentation 的示例:

<set-payload value="Hello world" />
<http:request request-config="HTTP_Request_Configuration"
      path="test" method="GET" sendBodyMode="ALWAYS"  />

所以对于你的情况,你想要:

<set-payload value="#[flowVars.xmlData]"/>

<http:request config-ref="HTTP_Request_Configuration1"
      path="/#flowVars.outputAppName]/services/EmployeeService/importUsers"
      method="POST" parseResponse="false"
      sendBodyMode="ALWAYS">
  <http:request-builder> 
    <http:query-param paramName="serviceId" value="#[flowVars.outputServiceID]"/>
    <http:query-param paramName="expiresOn" value="#[flowVars.expiresOnImport]"/>
    <http:query-param paramName="importId" value="#[flowVars.importId]"/>
    <http:query-param paramName="signature" value="#[flowVars.signatureImport]"/>
  </http:request-builder>
</http:request>

【讨论】:

    【解决方案2】:

    我建议查看提供的 http 路径的定义 如下 /#flowVars.outputAppName]/services/EmployeeService/importUsers

    您可以尝试重新定义如下
    #['/'+flowVars.outputAppName+'/services/EmployeeService/importUsers']

    另一件事就像@David建议的那样 使用 set-payload 组件设置 xml 数据 并使用所需的一组查询参数和 http 连接器 method="POST"

    定义 http 组件

    【讨论】:

      【解决方案3】:

      不知道这是否有帮助,但我正在寻找一种方法来刷新 xml 数据并进入这个主题。可能无法准确回答这个问题,但如果有人像我一样来到这里寻找相同疑问的答案:

      从响应中刷新 xml 数据的一种方法是:

      ...使用 Axis2 + SOAP 1.2

      OMElement omElement = payloadTypeResponse.getOMElement(ServiceEMEStub.QueryData.MY_QNAME,OMAbstractFactory.getSOAP12Factory());
      System.out.println(omElement.toString());
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-24
        • 2015-06-08
        • 2021-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多