펌 - https://www.ibm.com/support/knowledgecenter/SSBLQQ_8.7.1/com.ibm.rational.rit.ref.doc/topics/r_ritref_rest_api_ex.html
Example of calling REST API with Java HTTP
The following example uses Apache HttpClient v4 to call a REST API.
Example
import java.io.*;
import java.net.*;
import javax.xml.xpath.*;
import org.apache.http.*;
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.client.utils.*;
import org.apache.http.entity.*;
import org.apache.http.impl.client.*;
import org.apache.http.util.*;
import org.xml.sax.*;
...
String stubsApiBaseUri = "http://localhost:7819/RTCP/rest/stubs/";
String domain = "default";
String environment = "addNumbers";
String stubName = "1+1=2";
HttpClient client = HttpClients.createDefault();
URIBuilder builder = new URIBuilder(stubsApiBaseUri);
builder.addParameter("domain", domain);
builder.addParameter("env", environment);
builder.addParameter("stub", stubName);
String listStubsUri = builder.build().toString();
HttpGet getStubMethod = new HttpGet(listStubsUri);
HttpResponse getStubResponse = client.execute(getStubMethod);
int getStubStatusCode = getStubResponse.getStatusLine()
.getStatusCode();
if (getStubStatusCode < 200 || getStubStatusCode >= 300) {
// Handle non-2xx status code
return;
}
String responseBody = EntityUtils
.toString(getStubResponse.getEntity());
// Assuming only one stub matches
String stubRelativeUri = XPathFactory
.newInstance()
.newXPath()
.evaluate("/stubs/stub/@href",
new InputSource(new StringReader(responseBody)));
String stubAbsoluteUri = new URI(stubsApiBaseUri).resolve(
stubRelativeUri).toString();
HttpPost startStubMethod = new HttpPost(stubAbsoluteUri);
ContentType contentType = ContentType.APPLICATION_XML
.withCharset("utf-8");
String data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<start-stub />";
startStubMethod.setEntity(new ByteArrayEntity(data
.getBytes(contentType.getCharset()), contentType));
HttpResponse startStubResponse = client.execute(startStubMethod);
int startStubStatusCode = startStubResponse.getStatusLine()
.getStatusCode();
if (startStubStatusCode < 200 || startStubStatusCode >= 300) {
// Handle non-2xx status code
return;
}
// If you want to check the status of the stub that is starting, you
// can use the response data to get the stub instance URI and poll it
// for updates
System.out.println(startStubStatusCode);
String startStubResponseBody = EntityUtils.toString(startStubResponse
.getEntity());
System.out.println(startStubResponseBody);
} catch (URISyntaxException | IOException | XPathExpressionException e) {
// Handle errors
}
'자바스토리' 카테고리의 다른 글
csv 다운로드 (0) | 2017.12.05 |
---|---|
POI 3.15를 사용한 엑셀 다운로드 (0) | 2017.11.10 |
날짜 표현하기 SimpleDateFormat (0) | 2017.05.30 |
Serializable 과 transient (0) | 2016.03.09 |
URL 에서 jsessionid 제거 방법 (0) | 2014.11.22 |