Getting Started with Service API
This page will explain how to get started through some short examples that generate a default REST interface for a DataElement. More extensive documentation on API Integration can be found here.
The service API expandersExpanderA unit of generation that combines a data tree instance (functional M1 model) with a template to generate an artifact. can generate a full implementation for a REST API. While by default there is extensive code generation, the developer still has to write some custom code to provide the needed logic for the REST API. This is due to the absence of a meta-model to represent the REST API at this time.
Setting up your project
To get started, you have to add the service API expandersExpanderA unit of generation that combines a data tree instance (functional M1 model) with a template to generate an artifact. as an expansion resourceExpansion Resource (ExpansionResource). A reusable resource bundle containing expanders and features that provide specific application capabilities, defined in the expansion settings and added to the classpath during expansion. to your applicationApplicationThe overarching model container grouping all components, domain logic, and configurations for an enterprise solution. project. This can be done either in the µRadiant, or directly in the expansion settingsExpansion Settings (ExpansionSettings). The core XML configuration file specifying model locations, target expansion output directories, and expansion resources. file.
µRadiant
- Open the model for your JEE application.
- Go the to settings tab.
- Add a new expansion resource with name
org.normalizedsystems.beam:service-api-beamand version2.0.2.
Project files
- Open the expansion settings file for your application, which is often found in the
conffolder in the root of your project's workspace with the nameexpansionSettings.xml. - Add a child element to
expansionResourcesas follows:<expansionResource name="org.normalizedsystems.beam:service-api-beam" version="2.0.2"/>
Automatic API provisioning
After adding service-api-beam to a project, new transmutations will become available to automatically provision default API implementations for an existing model. These can be executed through right-clicking the element model element in the µRadiant, where the transmutation will be listed in the menu, or through command-line using Maven.
ComponentThis transmutation will add a default REST API to an entire component. That means that it will add the default Component options such as enableJaxrs and then run the AddDefaultRestApi transmutation on every DataElement in that Component.
DataElementThis transmutation will enrich the model for a single DataElement will all of the model elements needed to fully expose the element through a default connector implementation with all fieldsFieldA property of a DataElement expanded to a database column and user interface input element. in the element being exposed one-to-one through the API.
Where possible, the transmutation takes into account the current state of the model for the DataElement and provisions it accordingly. For example, if you already set a functional key to a fieldFieldA property of a DataElement expanded to a database column and user interface input element. with a specific name, it will use that fieldFieldA property of a DataElement expanded to a database column and user interface input element. and key instead of adding the default externalId fieldFieldA property of a DataElement expanded to a database column and user interface input element..
The transmutation only applies changes if the option enableJaxrs is present on the Component of which the DataElement is part.
If your model violates some of the base requirements of the rest-expandersExpanderA unit of generation that combines a data tree instance (functional M1 model) with a template to generate an artifact., the transmutation will not run! This includes using multiple fieldsFieldA property of a DataElement expanded to a database column and user interface input element. or a non-string fieldFieldA property of a DataElement expanded to a database column and user interface input element. as the functional key.
Manually modifying the model
Your first connector
- Create or choose a Component for the REST interface in your applicationApplicationThe overarching model container grouping all components, domain logic, and configurations for an enterprise solution..
- Add the option
enableJaxrs,enableSwaggerandenableValidationto the Component. - Add a DataElement to the Component which you wish to expose through the API. Give it a name like
Example. - Add the option
includeJaxrsConnectorto the DataElementExample, with the option value/examples/{exampleId}. - Add the options
exposeGetEndpointandexposeGetListEndpointto the DataElementExample. - Add the fieldsFieldA property of a DataElement expanded to a database column and user interface input element.
nameanduuidwith typeStringto the DataElementExample. - Add the options
generateUuidandisUniqueto theuuidfieldFieldA property of a DataElement expanded to a database column and user interface input element., without any option values. - Add the finderFinderA search specification combining field-operator pairs to execute standard data queries on a DataElement.
findByUuidEqto the DataElementExample. - Add the option
exposeRestFieldto thenameanduuidfieldsFieldA property of a DataElement expanded to a database column and user interface input element. of the DataElementExample. - Add the option
functionalKeywith valueuuidto the DataElementExample. - Expand, build and deploy.
Your API for the Example DataElement is now available at path /applicationName/v1/examples. You can view the API documentation for the API at /applicationName/swagger/componentName.
Adding input endpoints
Every input endpoint (POST, PUT, PATCH, DELETE) can be defined as a combination of an option and a DataCommand with a specific name. The implementation of the logic for these endpoints must be done in custom code in their respective CommandExtension classes, except when generating default implementations.
POST endpoint
To add a POST endpoint to the DataElement Example:
- Add the option
exposePostEndpointto the DataElementExample. - Add the DataCommand
createExampleto the DataElementExample. - Add the option
jaxrs.command.createto the DataCommand. - In the DataCommand, add ValueField
name, with the typeString. - Expand, build and deploy.
PUT endpoint
To add a PUT endpoint to the DataElement Example:
- Add the option
exposePutEndpointto the DataElementExample. - Add the DataCommand
modifyExampleto the DataElementExample. - Add the option
jaxrs.command.modifyto the DataCommand. - In the DataCommand, add ValueFieldsFieldA property of a DataElement expanded to a database column and user interface input element.
exampleIdandname, both with the typeString. - Expand, build and deploy.
PATCH endpoint
To add a PATCH endpoint to the DataElement Example:
- Add the option
exposePatchEndpointto the DataElementExample. - Add the DataCommand
updateExampleto the DataElementExample. - Add the option
jaxrs.command.updateto the DataCommand. - In the DataCommand, add ValueFieldsFieldA property of a DataElement expanded to a database column and user interface input element.
exampleIdandname, both with the typeString. - Expand, build and deploy.
DELETE endpoint
To add a DELETE endpoint to the DataElement Example:
- Add the option
exposeDeleteEndpointto the DataElementExample. - Add the DataCommand
removeExampleto the DataElementExample. - Add the option
jaxrs.command.removeto the DataCommand. - In the DataCommand, add the ValueField
exampleIdwith the typeString. - Expand, build and deploy.
Marking a field as unique
A FieldFieldA property of a DataElement expanded to a database column and user interface input element. can be marked as unique, which will introduce additional uniqueness validations.
To mark the Example.name FieldFieldA property of a DataElement expanded to a database column and user interface input element. as unique:
- Add the option
isUniqueto the FieldFieldA property of a DataElement expanded to a database column and user interface input element.namein the DataElementExample. - Add the finderFinderA search specification combining field-operator pairs to execute standard data queries on a DataElement.
findByNameEqandfindByNameEq_UuidNeto the DataElementExample. - Expand, build and deploy.