如何使用mule中的HTTP连接器刷新请求中的xml数据

时间:2015-03-09 06:05:36

标签: java mule mule-studio mule-component

我正在使用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连接器中的其他参数一样。它也不起作用。

我做错了什么,或者有什么方法可以用骡子方式做到这一点?就像在请求中发送大型xml一样?

3 个答案:

答案 0 :(得分:0)

您需要在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>

答案 1 :(得分:0)

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

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

另一件事是@David建议的 使用set-payload组件设置xml数据 并使用所需的查询参数集和http连接器方法=“POST”

定义http组件

答案 2 :(得分:0)

不知道这是否有帮助,但我正在寻找一种方法来刷新xml数据并得到了这个主题。可能无法完全回答这个问题,但如果有人来这里,因为我一直在寻找同样的疑问,我的答案是:

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

...使用Axis2 + SOAP 1.2

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