2020年3月6日 星期五

Maven Call SOAP sample

At pos.xml add

<dependency>
    <groupId>com.mashape.unirest</groupId>
    <artifactId>unirest-java</artifactId>
    <version>1.4.9</version>
</dependency>

At controller add method

import com.mashape.unirest.http.Unirest;

String soap_postdata = "{\"DATA1\":\"XXX\",\"DATA2\":\"XXX\"}";
HttpResponse<JsonNode> jsonResponse = Unirest.post(soap_url)
  .header("cache-control", "no-cache")
  .header("accept", "application/json")
  .body(soap_postdata)
  .asJson();
if (jsonResponse != null) {
 if (jsonResponse.getStatus() == 200) {
    JSONObject json = jsonResponse.getBody().getObject();
       if (json.has("error")) {
   //SOAP Fail. json.has error
} else {
           //do it
        }
  }
}

補充說明:
呼叫後會有一堆這類的紀錄,貌似關不掉可以透過log關閉方式排除
[Thread-46] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Closing expired connections
[Thread-46] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Closing connections idle longer than 30 SECONDS

At logback.xml add
<logger name="com.mashape.unirest" level="ERROR" />

沒有留言: