Java REST Client

A very simple REST client.

License

License

GroupId

GroupId

org.keeber
ArtifactId

ArtifactId

simple-rest
Last Version

Last Version

0.9.5
Release Date

Release Date

Type

Type

jar
Description

Description

Java REST Client
A very simple REST client.
Project URL

Project URL

https://github.com/json-k/simple-rest
Source Code Management

Source Code Management

https://github.com/json-k/simple-rest

Download simple-rest

How to add to project

<!-- https://jarcasting.com/artifacts/org.keeber/simple-rest/ -->
<dependency>
    <groupId>org.keeber</groupId>
    <artifactId>simple-rest</artifactId>
    <version>0.9.5</version>
</dependency>
// https://jarcasting.com/artifacts/org.keeber/simple-rest/
implementation 'org.keeber:simple-rest:0.9.5'
// https://jarcasting.com/artifacts/org.keeber/simple-rest/
implementation ("org.keeber:simple-rest:0.9.5")
'org.keeber:simple-rest:jar:0.9.5'
<dependency org="org.keeber" name="simple-rest" rev="0.9.5">
  <artifact name="simple-rest" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.keeber', module='simple-rest', version='0.9.5')
)
libraryDependencies += "org.keeber" % "simple-rest" % "0.9.5"
[org.keeber/simple-rest "0.9.5"]

Dependencies

compile (1)

Group / Artifact Type Version
com.google.code.gson : gson jar 2.8.1

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

Simple Rest

Java REST client (simple)

Philosophy

It seems like REST interfaces are everywhere, along with simple CuRL examples posting JSON content. It should be easy right?

Then I find myself in Java - adding a special repository, downloading a client to talk to the interface, creating objects, learning factories, translating from JSON to Java and back again. Then...just when everything looks fine I see an option I need missing from the Java client. It's right there in the CuRL examples...

This is a simple, one class, Java 1.6, thread safe, REST (like) client that uses Gson for serialization (because it's fab). That's it!

Maven

The project is available in the Maven Central Repository. For your Gradle build:

	compile 'org.keeber:simple-rest:+'

Quickstart

Create a client:

Rest.Client rest=Rest.newClient("http://httpbin.org/").header("User-Agent", "JSON-K");

Clients are creataed using a base URL - handy for a fixed endpoint.

Make a request and call get: the endpoint is the base plus request URL (in this case "http://httpbin.org/get").

Rest.Client.Response response=rest.request("get").get();
if(response.hasResult()){
	...
}

Hint: request objects can be reused (they should be thread safe too). Also different route params can be called with each call (route params also swap into the query string too (because why not)).

Rest.Client.Request request = rest.newRequest("get/{id}");
Rest.Client.Response response = request.get("id","1234322");

Default headers are copied from the REST instance when the request is created.

Form posts are easy, as are sending objects as the message body:

Rest.Client.Response response1 = request.post(new Rest.XForm().add("param1", "myvalue"));

Rest.Client.Response response2 = request.put(new TotallyGreatObject());

Notice that request objects can be called with different methods.

Basic responses should be handled automatically - content types with "text" return a String, "json" returns a JsonElement, and all others return an InputStream (please remember to close it):

Rest.Client.Response response = request.post(new Rest.MultipartForm().add("param1", "myvalue");
System.out.println(response.as(String.class);

Rest.Client.Response response = request.post(new Rest.XForm().add("param1", "some other value"));
response.as(InputStream.class);

Streaming things can be done by passing an input steam or by creating a Payload (an interface) via the io package.

Rest.Client.Response response = request.post(Rest.io.newPayload(is, 209889));

If the lengths of all of the streams are known, so the content length can be determined - the HTTP request is sent in streaming mode (there is no local caching before sending).

TODO

Probably change everything again (add more unit tests).

Versions

Version
0.9.5
0.9.4
0.9.3
0.9.2
0.9.0
0.8.0
0.7.0
0.6.0
0.5.2
0.5.1
0.5.0