23

GitHub - orange451/JRest: Lightweight Rest Library

 4 years ago
source link: https://github.com/orange451/JRest
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

JRest

Super small and lightweight Java REST Library. Only has one (optional) dependency, Gson (For POJO Serialization)!. It 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.

Why JRest?

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.

Examples

Simple GET request:

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

Simple GET request (async):

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 {

	public static void main(String[] args) {
		/**
		 * Start server
		 */
		JRest server = JRest.create()
				.setServerName("Test Server")
				.setPort(80)
				.start();
		
		/**
		 * Open in a web browser! http://localhost/
		 */		
		server.addEndpoint(HttpMethod.GET, "/", MediaType.TEXT_HTML, (request)->{
			return new ResponseEntity<String>(HttpStatus.OK, "<h1>Index! Welcome to JREST!</h1>");
		});
		
		/**
		 * Test Endpoint. Returns static String
		 */
		server.addEndpoint(HttpMethod.GET, "/testAPI", (request)->{
			return new ResponseEntity<String>(HttpStatus.OK, "Hello From Server!");
		});
	}
}

Serialize Maps to JsonObjects:

/**
 * SERVER CODE
 */
server.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"));
});

Using DTO/POJO objects:

public class TestDTO {

   public static void main(String[] args) throws MalformedURLException, IOException {
      // 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());
      });
   }
}

/**
 * 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;
   }
}

Host a webserver:

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

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK