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-stack
and version4.29.0
.
Project files
- Open the expansion settings file for your application, which is often found in the
conf
folder in the root of your project's workspace with the nameexpansionSettings.xml
. - Add a child element to
expansionResources
as follows:<expansionResource name="net.democritus:rest-jaxrs-stack" version="4.29.0"/>
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.
Component
This 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.
DataElement
This 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
,enableSwagger
andenableValidation
to 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
includeJaxrsConnector
to the DataElementExample
, with the option value/examples/{exampleId}
. - Add the options
exposeGetEndpoint
andexposeGetListEndpoint
to the DataElementExample
. - Add the fields
name
anduuid
with typeString
to the DataElementExample
. - Add the options
generateUuid
andisUnique
to theuuid
field, without any option values. - Add the finder
findByUuidEq
to the DataElementExample
. - Add the option
exposeRestField
to thename
anduuid
fields of the DataElementExample
. - Add the option
functionalKey
with valueuuid
to 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
exposePostEndpoint
to the DataElementExample
. - Add the DataCommand
createExample
to the DataElementExample
. - Add the option
jaxrs.command.create
to 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
exposePutEndpoint
to the DataElementExample
. - Add the DataCommand
modifyExample
to the DataElementExample
. - Add the option
jaxrs.command.modify
to the DataCommand. - In the DataCommand, add ValueFields
exampleId
andname
, both with the typeString
. - Expand, build and deploy.
PATCH endpoint
To add a PATCH
endpoint to the DataElement Example
:
- Add the option
exposePatchEndpoint
to the DataElementExample
. - Add the DataCommand
updateExample
to the DataElementExample
. - Add the option
jaxrs.command.update
to the DataCommand. - In the DataCommand, add ValueFields
exampleId
andname
, both with the typeString
. - Expand, build and deploy.
DELETE endpoint
To add a DELETE
endpoint to the DataElement Example
:
- Add the option
exposeDeleteEndpoint
to the DataElementExample
. - Add the DataCommand
removeExample
to the DataElementExample
. - Add the option
jaxrs.command.remove
to the DataCommand. - In the DataCommand, add the ValueField
exampleId
with 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
isUnique
to the Fieldname
in the DataElementExample
. - Add the finder
findByNameEq
andfindByNameEq_UuidNe
to the DataElementExample
. - Expand, build and deploy.