带参数的Mule HTTP端点

时间:2014-11-27 21:49:36

标签: http mule httpurlconnection

我试图编写Mule流来轮询CRUD-web服务。我设法做了一些我喜欢用小型JAVA应用程序做的事情,但我想在骡子流中使用HTTP端点,如果可能的话。

我想在HTTP-HEADER中使用数据库ID执行HTTP-GET请求。这是否可以使用MULE-inbound-endpoint?

    // HTTP GET request
private void sendGet() throws Exception {


    String url = "https://XXXXXXXXXXXXXXXXXXXXXXXXXX";

    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    // optional default is GET
    con.setRequestMethod("GET");
    con.setRequestProperty("X-Appery-Database-Id", "XXXXXXXXXXXXXXXXXX");

    //add request header
    con.setRequestProperty("User-Agent", USER_AGENT);

    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'GET' request to URL : " + url);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    //print result
    System.out.println(response.toString());

2 个答案:

答案 0 :(得分:1)

有可能,以下:

    <poll>
        <http:outbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" method="GET">
            <set-property propertyName="X-Appery-Database-Id" value="XXXXXXXXXXXXXXXXXX" />
            <set-property propertyName="User-Agent" value="MY_USER_AGENT" />        
        </http:outbound-endpoint>
    </poll>

将生成如下请求:

GET / HTTP/1.1
X-MULE_ENDPOINT: http://localhost:8081
X-Appery-Database-Id: XXXXXXXXXXXXXXXXXX
X-MULE_ROOT_MESSAGE_ID: 8c80b9f0-7682-11e4-94ea-12a3ae03ce3a
User-Agent: MY_USER_AGENT
X-MULE_SESSION: rO0ABXNyACNvcmcubXVsZS5zZXNzaW9uLkRlZmF1bHRNdWxlU2Vzc2lvbi7rdtEW7GGKAwAFWgAFdmFsaWRMAA1mbG93Q29uc3RydWN0dAAmTG9yZy9tdWxlL2FwaS9jb25zdHJ1Y3QvRmxvd0NvbnN0cnVjdDtMAAJpZHQAEkxqYXZhL2xhbmcvU3RyaW5nO0wACnByb3BlcnRpZXN0AA9MamF2YS91dGlsL01hcDtMAA9zZWN1cml0eUNvbnRleHR0ACdMb3JnL211bGUvYXBpL3NlY3VyaXR5L1NlY3VyaXR5Q29udGV4dDt4cAFwdAAkOGM4MGI5ZjEtNzY4Mi0xMWU0LTk0ZWEtMTJhM2FlMDNjZTNhc3IAJWphdmEudXRpbC5Db2xsZWN0aW9ucyRTeW5jaHJvbml6ZWRNYXAbc/kJS0s5ewMAAkwAAW1xAH4AA0wABW11dGV4dAASTGphdmEvbGFuZy9PYmplY3Q7eHBzcgAkb3JnLm11bGUudXRpbC5DYXNlSW5zZW5zaXRpdmVIYXNoTWFwndHZ72dFzgADAAB4cHcMP0AAAAAAABAAAAAAeHEAfgAJeHB4
Host: localhost:8081

答案 1 :(得分:1)

您可以使用MuleClient执行一组测试(CRUD操作),例如:

MuleClient client = muleContext.getClient();
Map parameters = new HashMap();
parameters.put("Content-Type", "application/json");
parameters.put("http.method", "GET");     // or POST, DELETE, PUT
parameters.put("DatabaseID", "ID");

然后发送到http-inbound-endpoint

MuleMessage response = client.send("http://localhost:8091/rest/domain", PAYLOAD, parameters);
assertNotNull(response);
assertThat(Integer.valueOf(response.getInboundProperty(HTTP_STATUS).toString()), is(200));

Victor告诉你如何从流程中完成它。我希望能提供帮助。