Getting started
This page will explain how to get started through some short examples that generate a default REST interface for a DataElement.
What to expect
The REST expanders can generate a full implementation for a REST API. These APIs will combine the REST protocol with several specifications and standard practices such as the HAL specification in JSON format. 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.
To ensure that we can provide a 100% generated API out of the box, we followed an approach similar to convention over configuration where we provide a solid default implementation for simple elements that will be sufficient in most cases. This is activated by using a uuid: String field as functional key, placing the resource identifiers in the includeJaxrsConnector option rather than in the options for the endpoints and by using the options exposeRestField, isUnique and isRequired for fields in a DataElement. This will couple the REST API to the data model, but this can be disabled on a per-field basis by not making use of those field options or for the entire DataElement by placing the resource identifiers in the options for the endpoints.
Setting up your project
To get started, you have to add the REST expanders as an expansion resource to your application project. This can be done either in the µRadiant, or directly in the expansion settings file.
µRadiant
- Open the model for your JEE application.
- Go the to settings tab.
- Add a new expansion resource with name
net.democritus:rest-jaxrs-stackand version5.2.1.
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="net.democritus:rest-jaxrs-stack" version="5.2.1"/>
Automatic API provisioning
After adding rest-jaxrs-stack 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 fields 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 field with a specific name, it will use that field and key instead of adding the default externalId field.
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-expanders, the transmutation will not run! This includes using multiple fields or a non-string field as the functional key.
Manually modifying the model
Your first connector
- Create or choose a Component for the REST interface in your application.
- 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 fields
nameanduuidwith typeStringto the DataElementExample. - Add the options
generateUuidandisUniqueto theuuidfield, without any option values. - Add the finder
findByUuidEqto the DataElementExample. - Add the option
exposeRestFieldto thenameanduuidfields 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 ValueFields
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 ValueFields
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 Field can be marked as unique, which will introduce additional uniqueness validations.
To mark the Example.name Field as unique:
- Add the option
isUniqueto the Fieldnamein the DataElementExample. - Add the finder
findByNameEqandfindByNameEq_UuidNeto the DataElementExample. - Expand, build and deploy.