2015-01-31 2 views
1

Мы пытаемся подключить HTTP-исходящий запрос к веб-службе, где для URL требуется простой POST.Spring-интеграция http-outbound-gateway для веб-службы xml, не возвращающей данные ответа

Когда мы пытаемся с помощью HTTP-исходящее-шлюз для этого веб-сервиса следующим

Веб-сервис, вероятно, называется так как никакой ошибки, однако ответ мы получаем не так

{ 
    "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" 
} 

Затем мы попытались использовать следующий образец кода в элементе трансформатора и вызвали ли он по адресу РАЗЛИЧНЫЙ элемент http-inboundbound. После этого у нас была выше HTTP-исходящее элемент гиперссылкой на HTTP-въездной элемент, который вызывает трансформатор, как показано

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; 
} 

выше фрагмент кода будет вызывать ФАКТИЧЕСКУЮ веб-сервис и получить успешный ответ. Затем мы вернуть XML-ответ обратно в основной HTTP-исходящее элемент

Однако мы не заладилась, и до сих пор новый ответ, который мы получили от «данных-шлюз» был

{ 
    "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" 
} 

заметить также, что JSON-сервер значение атрибута теперь причал, который является нашим собственным сервером.

Ниже приведена полная интегральная пружина 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" /> 

ответ

2

Вам необходимо настроить ожидаемый тип ответа на исходящем шлюзе; например .:

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

В противном случае, результат является HttpResponse объектом с null тела.

Смежные вопросы