Since you develop API you need endpoints and controllers. Endpoint is a point at which an API (the code that allows two software programs to communicate with each other) connects with the software program. Due to writing an endpoint is a routine, JBT allows to reduce boilerplate code. Controller's main aim is to accept an input and to convert it to commands for the model or view. Since you work with API, view is an endpoint.
JBT uses XML-files for describing controllers, endpoints and models. Then it uses such files for Java-code generation. As a result you get API based on MVC pattern written on Java. Let's start.
First of all, you need to create a controller. Let's take an API for working with companies as an example.
First step is to create a folder for our API in api-spec module of JBT, so our path will be api/api-spec/src/main/resources/spec/api. Here a folder named 'company' is created.
Then put the file 'controller.xml' in created directory. Here it is, controller created, let's describe it.
IMPORTANT: controller must have exactly the name specified above. Also folder must be created using the path specified above.
The file must contain the following tags:
Tag <overview> must contain tag <description> where you should write a short description of a controller.
Tag <public> usually contains tags <include>. In <include> you should specify operation's name e.g. 'getFirmInformation' (further a folder with this name should be created for an endpoint) as 'value' attribute. Also you may use tag attribute 'weight' with a number passed to it. This attribute is optional and is used for placing @Weight annotation over the generated method with specified value. More info about mentioned attributes and their purpose here.
Tag <private> must also have tag <include>, but also it specifies access level to endpoints, so it has attribute 'alias' for it. It provides the following access levels:
There are 'jwt', 'basic' and 'apiKey' with true/false values among other optional attributes in tag <private>. You can find more info about these attributes and their purpose here.
Description example for a controller is given below:
<controller xmlns="http://www.knubisoft.com/api/schema/controller"> <overview> <description> Company API </description> </overview> <public> <include value="getCompany"/> <include value="getEmployee" weight="2"/> </public> <private alias="baseRights"> <include value="addFirmInformation"/> <include value="getFirmInformation"/> <include value="addCreditCard"/> <include value="updateInformation"/> </private> </controller> |
Basically, you have different operations in
As a result you should get the following structure: