JREST - Lightweight REST Library

This is a small project I created while making a Runescape bot. I needed a clean way to have multiple bots communicate with eachother, and I wanted it to work in a RESTful way. Spring Boot takes a year to turn on, so I needed something fast, and most other libraries require a million dependencies.

So here’s this fun little thing I whipped up in 2 days. It has fast startup time, and only requires Gson as a dependency.
Please don’t use this in production code… This is just a hobby project! Use something that’s been tried for much longer!

GITHUB:

Here’s some examples:

Synchronous Get:

RequestEntity<String> request = new RequestEntity<>(HttpMethod.GET);
ResponseEntity<String> response = request.exchange("http://localhost/testAPI", String.class);
System.out.println(response.getBody());

Asynchronous Get:

RequestEntity<JsonObject> request = new RequestEntity<>(HttpMethod.GET);
request.exchangeAsync("http://localhost/testJson", JsonObject.class, (response)->{
	System.out.println(response.getBody());
});

Simple REST Server:

public class TestServer extends RestServer {
	public TestServer() {
		
		/**
		 * Test Endpoint. Returns static String
		 */
		this.addEndpoint(HttpMethod.GET, "/testAPI", (request)->{
			return new ResponseEntity<String>(HttpStatus.OK, "Hello From Server!");
		});
		
		/**
		 * Test Post endpoint. Returns your posted data back to you.
		 */
		this.addEndpoint(HttpMethod.POST, "/testPost", MediaType.ALL, MediaType.ALL, (request)->{
			return new ResponseEntity<String>(HttpStatus.OK, request.getBody().toString());
		});
		
		/**
		 * Test JSON endpoint. Returns a JSON object.
		 */
		this.addEndpoint(HttpMethod.GET, "/testJson", MediaType.ALL, MediaType.APPLICATION_JSON, (request)->{
			JsonObject jsonObject = new JsonObject();
			jsonObject.addProperty("TestKey", "Hello World!");
			
			return new ResponseEntity<JsonObject>(HttpStatus.OK, jsonObject);
		});
	}
	
	@Override
	public int getPort() {
		return 80;
	}

	public static void main(String[] args) {
		new TestServer();
	}
}

Specifying Generic Body/Return Types:


/**
 * SERVER CODE
 */
this.addEndpoint(HttpMethod.POST, "/GetEmployee", JsonObject.class, (request)->{
	JsonObject payload = request.getBody();
	int id = payload.get("id").getAsInt();
	
	String[] names = {
			"Frank",
			"Jeff",
			"Oliver",
			"Maxwell"
	};
	
	JsonObject response = new JsonObject();
	response.addProperty("id", id);
	response.addProperty("name", names[id-1]);
	
	return new ResponseEntity<JsonObject>(HttpStatus.OK, response);
});

/**
 * CLIENTCODE
 */
JsonObject body = new JsonObject();
body.addProperty("id", 1);
RequestEntity<JsonObject> request = new RequestEntity<>(HttpMethod.POST, body);
request.exchangeAsync("http://localhost/GetEmployee", JsonObject.class, (response)->{
	JsonObject payload = response.getBody();
	System.out.println("Employee data: ");
	System.out.println("\tid: " + payload.get("id").getAsInt());
	System.out.println("\tname: " + payload.get("name"));
});

DTO Serialization (using Gson):

public class TestDTO {

	public TestDTO() throws MalformedURLException {
		// Create payload
		JsonObject body = new JsonObject();
		body.addProperty("id", 1);
		
		// Create request object
		RequestEntity<JsonObject> request = new RequestEntity<>(HttpMethod.POST, body);
		
		// Send request to server
		request.exchangeAsync("http://localhost/GetEmployee", Employee.class, (response)->{
			Employee employee = response.getBody();
			System.out.println("Employee data: ");
			System.out.println("\tid: " + employee.getId());
			System.out.println("\tname: " + employee.getName());
		});
	}

	public static void main(String[] args) throws MalformedURLException, IOException {
		new TestDTO();
	}
}

/**
 * DTO used to represent employee information sent from server.
 */
class Employee {
	private int id;
	
	private String name;
	
	public Employee() {
		//
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public String getName() {
		return this.name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
}

Also works like a webserver:

/**
 * SERVER CODE
 */
this.addEndpoint(HttpMethod.GET, "/", MediaType.TEXT_HTML, (request)->{
	return new ResponseEntity<String>(HttpStatus.OK, "<h1>Index! Welcome to JREST!</h1>");
});

Little update, added support for application/x-www-form-urlencoded