You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Object relation mapping library for REST API:s in Java. It is designed to build applications that are powered by a RESTful API instead of a database.
4
+
Java-ormen is an Object relation mapping library for REST API:s in Java. It is designed to build applications that are powered by a RESTful API instead of a database.
5
5
6
-
The Async java-ormen library purpose is to allow Java applications to easily execute HTTP requests and asynchronously process the HTTP responses, without the developer needing to think about the http requests.
6
+
Thos async library purpose is to allow Java applications to easily execute HTTP requests over an REST API, and create an abstraction on top of REST, so that only the CRUD operations are exposed.
7
7
8
-
The library creates an abstraction layer on top of REST. The user of this library will in fact know very little about what protocol is used.
9
-
10
-
The library has been designed using the Future API. If you you don't need a ascynchronous library you can tell the library to execute query's synchronous.
8
+
The library has been designed using the Java CompletableFuture. If you you don't need a ascynchronous library you can tell the library to execute query's synchronous.
11
9
12
10
The library uses the Jackson ObjectMapper, so its possible to use jackson annotations to describe how objects shall be marshalled and unmarshalled.
13
11
14
-
The library strives for creating immutable objects.
12
+
The library strives for creating immutable objects. This means that you can have control over your objects.
15
13
16
14
## Usage
17
15
18
16
Lets say that we have a REST API that exposes CRUD Operations for fetching Dog´s. Well dogs can bark, so we will use dogs in this example.
19
-
20
-
As an developer you will probably create some POJOS for a java representation of the Dogs. Then you will probably write some kind of handler that takes these objects and sends them
17
+
18
+
As an developer you will probably create some POJOś as an a java representation of the Dogs. Then you will probably write some kind of handler that takes these POJO instance´s and sends them
21
19
22
20
back and forth the REST API. Well, you will not need to do that any longer.
23
21
24
22
25
23
One just simply do the following:
26
24
27
-
Start of by creating a normal POJO, but we will make sure it extends AbstractRestModel. This will give the POJO super powers ( almost ). Well it will give the pojo possibility interact with an rest api.
25
+
Start of by creating a normal POJO, but we will make sure it extends RestModel. This will give the POJO super powers ( almost ). Well it will give the pojo possibility interact with an rest api.
28
26
29
-
The AbstractRest model gives you 4 public methods for interacting with the rest api, one for each CRUD operation. The good part here is that these methods takes an ActionCompletedinterface as parameter, so you can work with async request. More about that later. here is the code.
27
+
The Rest model gives you 4 public methods for interacting with the rest api, one for each CRUD operation. The good part here is that these methods returns a Completable future.
30
28
29
+
Look at the comments, it will give you some hints about what information you need to provide for successfully send requests over rest.
31
30
32
31
```java
33
32
34
-
publicclassDogModelextendsRESTModel {
33
+
publicclassMockRestModelextendsRESTModel {
35
34
36
-
publicfinalString name;
37
-
38
-
publicDogModel() {
35
+
/**
36
+
* Default constructor, this is needed by the jackson json mapper. so always include it.
37
+
**/
38
+
publicMockRestModel() {
39
39
this.name ="";
40
40
}
41
-
publicDogModel(Stringname) {
41
+
42
+
publicMockRestModel(Stringname) {
42
43
this.name = name;
43
44
}
44
-
45
+
/**
46
+
*
47
+
* The return value of this method will indicate the resource url. The resource url points to the collection of resources that the pojo targets
48
+
**/
45
49
@Override
46
50
StringresourceUrl() {
47
51
return"dogs";
48
52
}
49
-
53
+
/**
54
+
* This method returns the identifying data for this resource. for example this might be this.id or this.name or something else.
55
+
*
56
+
**/
50
57
@Override
51
58
StringidentifierValue() {
52
59
@@ -56,50 +63,36 @@ public class DogModel extends RESTModel {
56
63
57
64
58
65
}
59
-
60
66
```
61
67
62
-
63
-
The Dog model has a method called ```resourceUrl```. This url will point on to the url location where we can work on dogs. The Dog model has a method called ```identifierValue```. It will be appended on the url on RUD operations. The good part here is that we will not need to write any marshall or unmarshalling logic. This is handled the RestModel class. But if you get into truble you can use jackson json annotations on your fields.
68
+
The good part with this model is that we will not need to write any marshall or unmarshalling logic. This is handled by the RestModel class. But if you get into trouble you can use jackson json annotations on your fields. For example there might be times when you want to exclude some data from de json marshalling, or perhaps rename.
64
69
65
70
66
71
So now we have created a model. Its now time to use it to fetch data. Simply write the following in your code. Remember, these operations are async so we will add a sleep in the end ( only for educational purpose ).
In the code above, the dog model will call the rest api when the fetch method is invoked. When the server responds the onDone or onError will be fired.
93
+
In the code above, the dog model will call the rest api when the fetch method is invoked. A CompletableFuture is returned, and we can work with that future as an promise.
0 commit comments