Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (807)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (872)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  JREST - Lightweight REST Library  (Read 697 times)
0 Members and 1 Guest are viewing this topic.
Offline orange451

JGO Kernel


Medals: 571
Projects: 9
Exp: 8 years


Your face? Your ass? What's the difference?


« Posted 2020-04-30 16:56:48 »

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:
https://github.com/orange451/JRest

Here's some examples:

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


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


Simple REST Server:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
/**
 * 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):
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
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:
1  
2  
3  
4  
5  
6  
/**
 * SERVER CODE
 */

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


First Recon. A java made online first person shooter!
Offline orange451

JGO Kernel


Medals: 571
Projects: 9
Exp: 8 years


Your face? Your ass? What's the difference?


« Reply #1 - Posted 2020-05-01 22:10:37 »

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

First Recon. A java made online first person shooter!
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (843 views)
2019-09-04 15:33:17

hadezbladez (5778 views)
2018-11-16 13:46:03

hadezbladez (2597 views)
2018-11-16 13:41:33

hadezbladez (6195 views)
2018-11-16 13:35:35

hadezbladez (1494 views)
2018-11-16 13:32:03

EgonOlsen (4729 views)
2018-06-10 19:43:48

EgonOlsen (5779 views)
2018-06-10 19:43:44

EgonOlsen (3270 views)
2018-06-10 19:43:20

DesertCoockie (4171 views)
2018-05-13 18:23:11

nelsongames (5481 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!