A simple fluent API to write calls to a REST API service without relying on additional libraries. It doesn't need or use Jax-RS client as it's using Java's built-in HttpUrlConnection.
It's intended purpose is to write simple one-line requests for testing, i.e. for preparing a service via Rest API for a test or reading the state after a test.
String resultJson = SimpleRestClient.requestTo("http://my.domain.com/rest/api/resource")
.acceptJson() //accept header
.get()
.asString();
The following snipped uses javax.json JsonWriter to serialize a JSON object onto the OutputStream.
String result = SimpleRestClient.requestTo("http://my.domain.com/rest/api/resource")
.acceptJson() //accept header
.sendJson() //conent type header
.post(os -> {
try (JsonWriter w = Json.createWriter(os)) {
w.writeObject(jsonObject);
}
})
.asString();
SimpleRestClient.requestTo("http://my.domain.com/rest/api/resource")
.accept("text/html")
SimpleRestClient.requestTo("http://my.domain.com/rest/api/resource")
.contentType("text/html")
SimpleRestClient.requestTo("http://my.domain.com/rest/api/resource")
.addHeader("ContentLength", "20")
Authentication in general (if you have your auth token)
String resultJson = SimpleRestClient.requestTo("http://my.domain.com/rest/api/resource")
.auth("Basic QWxhZGRpbjpPcGVuU2VzYW1l")
.acceptJson()
.get()
.asString();
Basic Authentication
String resultJson = SimpleRestClient.requestTo("http://my.domain.com/rest/api/resource")
.basicAuth("username","password")
.acceptJson()
.get()
.asString();
(using javax.json
)
Add the following dependency to your pom
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.0</version>
</dependency>
Now read the inputstream from the response
JsonObject json = Json.createReader(SimpleRestClient.requestTo("http://my.domain.com/rest/api/resource")
.acceptJson()
.get()
.asInputStream())
.readObject();