1
votes

We are trying to connect a http-outbound-request to a web service where the url requires a simple POST.

When we try using the http-outbound-gateway for this the web service as follows

The web service is probably called since there is no error however the response we get is as follows

{
    "headers": {
        "Date": [
            "Sat, 31 Jan 2015 08:35:14 GMT"
        ],
        "Server": [
            "Apache-Coyote/1.1"
        ],
        "Content-Type": [
            "text/xml;charset=UTF-8"
        ],
        "Content-Length": [
            "234"
        ]
    },
    "body": null,
    "statusCode": "OK"
}

We then tried using the following sample code in a transformer element and had it called by A DIFFERENT http-inbound element. Afterwards we had the above http-outbound element hyperlinked to the http-inbound element which which calls the transformer as shown

public String sendRequest(Message<?> data) {
    System.out.println(data);
    String allData = "";

    try {
        URL url = new URL("https://www.someurl.com/service");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setDoOutput(true);
        conn.setRequestMethod("POST");

        //Add headers from http outbound gateway
        for(Entry<String, Object> that : data.getHeaders().entrySet()){
            String key = that.getKey();
            Object value = that.getValue();

            if(Arrays.asList(HTTP_REQUEST_HEADER_NAMES).contains(key)){
                System.out.println("ADDING : " + key + " = " + value);
                conn.setRequestProperty(key, value.toString());
            }
        }

        OutputStream os = conn.getOutputStream();
        os.write(((String) data.getPayload()).getBytes());
        os.flush();

        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

        String output;
        while ((output = br.readLine()) != null) {
            allData += output;
        }

        conn.disconnect();

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println(allData);
    return allData;
}

The above piece of code would call the ACTUAL web service and get a successful response. We then return the response xml back to the main http-outbound element

However we had no luck and still the new response we got from "data-gateway" was

{
    "headers": {
        "Cache-Control": [
            "no-cache"
        ],
        "Content-Type": [
            "text/plain;charset=ISO-8859-1"
        ],
        "Content-Length": [
            "234"
        ],
        "Server": [
            "Jetty(8.1.14.v20131031)"
        ]
    },
    "body": null,
    "statusCode": "OK"
}

Also notice that the server json attribute value is now jetty which is our own server.

The following is the complete integration spring xml.

    <!-- MAIN FLOW -->
        <int:channel id="requestChannel"/>
        <int:channel id="responseChannel"/>

        <int-http:inbound-gateway supported-methods="POST"
            request-channel="requestChannel"
            reply-channel="responseChannel"
            path="/services/testData"
            reply-timeout="50000" />

        <int:transformer input-channel="requestChannel" output-channel="requestDataChannel" ref="requestGenerate" method="createRequest" />

        <int:channel id="requestDataChannel" />

        <int-http:outbound-gateway id="data-gateway"
                                   request-channel="requestDataChannel" 
                                   reply-channel="requestDataDisplayChannel"
                                   url="http://localhost:8080/rest-http/services/testRequest"
                                   <!-- we first tried directly calling the "https://www.someurl.com/service" directly from here which didn't work either -->
                                   http-method="POST"
                                   extract-request-payload="true"/>

        <int:channel id="requestDataDisplayChannel" />

        <int:transformer input-channel="requestDataDisplayChannel" output-channel="responseChannel" ref="requestGenerate" method="responseDisplay" />   




        <!-- TEST DUMMY WEB SERVICE WHICH ALSO CALLS THE ACTUAL WEB SERVICE SUCCESSFULLY THEN -->
        <int:channel id="requestSendChannel"/>
        <int:channel id="responseSendChannel"/>

        <int-http:inbound-gateway supported-methods="POST"
            request-channel="requestSendChannel"
            reply-channel="responseSendChannel"
            path="/services/testRequest"
            reply-timeout="50000" />

        <int:transformer input-channel="requestSendChannel" output-channel="responseSendChannel" ref="requestGenerate" method="sendRequest" />



        <!-- this is the class which contains all of the transformer java code including the java code shown above -->
        <bean name="requestGenerate" id="requestGenerate" class="org.application.RequestGenerate" />
1

1 Answers

2
votes

You need to configure the expected response type on the outbound gateway; e.g.:

expected-response-type="java.lang.String"

Otherwise, the result is an HttpResponse object with a null body.