Gemini Starter: Model Driven REST APIs in minutes

Andrea Tarquini
Javarevisited
Published in
4 min readSep 16, 2019

How to setup CRUD REST APIs starting from Data Model and without generating code by using the Gemini Framework (a Spring/Java platform)

Gemini is a Model Driven REST framework to automatically generate CRUD APIs. You can take a look at the official repository here.

We are going to define entities and code to obtain the following CRUD APIs.

This article explains what is included in the Gemini Starter repository and how to start with a full Gemini Project.

1. Gemini Custom Module

Gemini is made of a series of modules (core, auth, gui and so on..). When you start a project you can also provide your custom Module that is the container for your entities, code and APIs.

The previous code shows how to start with a Gemini Module. Gemini has two Spring Context: the root that loads entities and handles core features and the api context that starts the Web Application. So we use MyCustomModule to scan core components and events, while MyCustomModuleAPI is used to scan API module (that may be useful if you want to register totally custom Spring web/rest endpoints). But usually you don’t need them to start with Gemini basic REST features.

The important thing is that the core bean must override the Module Gemini Interface.

2. Gemini DSL and Entities

Since the name of the custom module is CUSTOM_MODULE we can put the schema definition in the resources file schemas/CUSTOM_MODULE.at

In the repository we have:

Note that Employee has two entity references: to Company and Gender.

Gender is a special entity that implements a Domain (a Gemini built in interface that provides two fields: code and description).

3.Immutable Entities

Immutable Entities are entities that cannot be modified, so it is not permitted to add a record (POST) or modify an existing one (PUT). But how can Gemini initialize them? It is simple, just add all the record inside the module record initialization file records/CUSTOM_MODULE.atr

In the previous file we have defined:

  • the domain values for the Gender Entity
  • and we made Gender Immutable specifying that is a closed domain.

NB: In Gemini each Entity has a lot of meta data associated with itself. All these meta are stored in the Entity called Entity. Yes it is, and you can query all the meta data with Swagger using the Entity endpoint.

So after Gemini complete its startup you have already the Gender filled with the value initialized in the file.

And if you take a look at the Gender APIs you can see that you cannot POST / PUT / DELETE . It is a closed immutable domain. Just GET Gender records.

4. Calculated field using Gemini Events

CRUD APIs are useful but sometimes you want to add some logic or custom behavior like validation or calculated field or records that automatically create other records as a dependency.

For example, let make the fullName a calculated field by the concatenation of firstName and lastName. We are working on the EMPLOYEE entity and we register the event before the record is inserted (create) the first time and also each time the records changes (update).

It is simple. You need the only a combination of Gemini annotations and the function to extract the fullName.

As you can see, if we POST a new Entity the fullName is automatically calculated.

Finally

In this article, I show you how to easily start with Gemini. But a lot of improvements will be developed, so I suggest to follow me and the main repository to be updated about each release.

--

--

Andrea Tarquini
Javarevisited

#software #developer and #engineer ➙ Proud #italian 🇮🇹 #geek ➙ #fullstack and #maker for fun