Super small and lightweight Java REST Library. Only has one (optional) dependency, Gson (For POJO Serialization)! JRest doesn't use annotations, doesn't require polymorphism, and can work asynchrounously. If JRest is ran without the Gson dependency, DTO/POJO objects cannot be serialized/deserialized but JSON Objects can still be serialized to maps using Javas' native Nashorn Javascript engine.
Many Java Rest libraries claim to be lightweight. How many require 0 dependencies, start up in less than 50 ms, are less than 50 kb in filesize, and only have 1 package? Enter JRest, the real lightweight Java Restful library. It can be used as a webhost, back-end server, or to make requests to already existing REST endpoints.
Simple GET request:
RequestEntity<String> request = newRequestEntity<>(HttpMethod.GET);
ResponseEntity<String> response = request.exchange("http://localhost/testAPI", String.class);
System.out.println(response.getBody());Simple GET request (async):
RequestEntity<JsonObject> request = newRequestEntity<>(HttpMethod.GET);
request.exchangeAsync("http://localhost/testJson", JsonObject.class, (response)->{
System.out.println(response.getBody());
});Simple Rest Server:
publicclassTestServer {
publicstaticvoidmain(String[] args) {
/** * Start server */JRestserver = JRest.create()
.setServerName("Test Server")
.setPort(80)
.start();
/** * Open in a web browser! http://localhost/ */server.addEndpoint(HttpMethod.GET, "/", MediaType.TEXT_HTML, (request)->{
returnnewResponseEntity<String>(HttpStatus.OK, "<h1>Index! Welcome to JREST!</h1>");
});
/** * Test Endpoint. Returns static String */server.addEndpoint(HttpMethod.GET, "/testAPI", (request)->{
returnnewResponseEntity<String>(HttpStatus.OK, "Hello From Server!");
});
}
}Serialize Maps to JsonObjects:
/** * SERVER CODE */server.addEndpoint(HttpMethod.POST, "/GetEmployee", JsonObject.class, (request)->{
JsonObjectpayload = request.getBody();
intid = payload.get("id").getAsInt();
String[] names = {
"Frank",
"Jeff",
"Oliver",
"Maxwell"
};
JsonObjectresponse = newJsonObject();
response.addProperty("id", id);
response.addProperty("name", names[id-1]);
returnnewResponseEntity<JsonObject>(HttpStatus.OK, response);
});
/** * CLIENTCODE */JsonObjectbody = newJsonObject();
body.addProperty("id", 1);
RequestEntity<JsonObject> request = newRequestEntity<>(HttpMethod.POST, body);
request.exchangeAsync("http://localhost/GetEmployee", JsonObject.class, (response)->{
JsonObjectpayload = response.getBody();
System.out.println("Employee data: ");
System.out.println("\tid: " + payload.get("id").getAsInt());
System.out.println("\tname: " + payload.get("name"));
});Using DTO/POJO objects:
publicclassTestDTO {
publicstaticvoidmain(String[] args) throwsMalformedURLException, IOException {
// Create payloadJsonObjectbody = newJsonObject();
body.addProperty("id", 1);
// Create request objectRequestEntity<JsonObject> request = newRequestEntity<>(HttpMethod.POST, body);
// Send request to serverrequest.exchangeAsync("http://localhost/GetEmployee", Employee.class, (response)->{
Employeeemployee = response.getBody();
System.out.println("Employee data: ");
System.out.println("\tid: " + employee.getId());
System.out.println("\tname: " + employee.getName());
});
}
}
/** * DTO used to represent employee information sent from server. */classEmployee {
privateintid;
privateStringname;
publicEmployee() {
//
}
publicintgetId() {
returnid;
}
publicvoidsetId(intid) {
this.id = id;
}
publicStringgetName() {
returnthis.name;
}
publicvoidsetName(Stringname) {
this.name = name;
}
}Host a webserver:
/** * SERVER CODE */server.addEndpoint(HttpMethod.GET, "/", MediaType.TEXT_HTML, (request)->{
returnnewResponseEntity<String>(HttpStatus.OK, "<h1>Index! Welcome to JREST!</h1>");
});Statistics:
- JRest library classes package to less than 50KB in filesize
- JRest servers can boot in less than 50 ms
- Does not inherently require dependencies outside of the JDK/JRE
- Average response time of "Hello World" get request is 3-4 ms (localhost).
