Last updated: 2024-07-04

  1. Learn
  2. Documentation
  3. Custom Controllers

Adding a custom REST API to your Spring Boot project

In a real application basic CRUD operations are often not enough. This is where the custom REST and frontend controllers of the Professional plan come into play.

The Controllers tab allows you and your team to agree on an API before writing a single line of code. Still the first prototype including tests is directly available for download after the concept has been completed. All the new paths can be selected by your security configs as well.

Let's extend our CarParts example and provide a small search API.

JSON structure of our new example request we're going to create

Defining the JSON structure

For our example we want to find CarParts that match our search parameters. For this purpose, a date range can be specified within which the spare part must have been released. In addition, a list of supplierIDs can be specified to further narrow down the result.

To enable this query, we first create the Data Objects in the respective tab. Later on we will select them at our endpoint, thus describing the JSON structure of the POST request. We start with the object "DateRange" and the two fields "releaseAfter" and "releaseBefore" with type OffsetDateTime.

Creating the DateRange object for our example endpoint

Creating the DateRange object for our example endpoint

Now we can create the second Data Object that describes our input: "SearchRequest" with the fields "dateRange" (with our just created Custom Type) and "supplierIds". For the latter we choose List with the list type Long.

Adding the SearchRequest with the DateRange custom field type

Adding the SearchRequest with the DateRange custom field type

Creating the endpoint

With this we can create our search controller. For our example we only need one endpoint with the HTTP method POST. As input type we select our created object "SearchRequest", as output we choose a List with the already existing type "CarDTO".

In addition to the type List, Page is also available as a response type. This adds pagination to the endpoint - that means only one page of the requested content is returned at once. With the parameters page, size and sort the request can be refined according to the Spring Boot standards.

Our new controller with the search endpoint

Our new controller with the search endpoint

We also add a parameter into the path of our controller: {clientId} so that the client can identify himself. The type selection is appearing right after the parameter has been added.

Additionally query parameters are available for the individual endpoints. So if we like, we can add another method searchByQuery with the path /?q= - this will add a @RequestParam(name = "q", required = false) final String q in our Java code.

We can discuss and improve the new API further within our dev team. The OpenAPI specification is directly available in Bootify - the button in the left upper corner of the Controllers tab opens a modal. If the Swagger option has been selected in the General tab, the new endpoint is also visible in Swagger UI (http://localhost:8080/swagger-ui.html) after downloading and starting the application.

The custom SearchController in Swagger UI

The custom SearchController in Swagger UI

The custom frontend controllers of the General tab are explained in the frontend settings. Our small example is already finished, so we can download the generated code and start with the actual implementation right away.

See Pricing
or read quickstart