Skip to content

Codegen

Billy Bolton edited this page Apr 9, 2024 · 42 revisions

Overview

Motivation

Building (RESTful) APIs in Java Spring Boot requires many of the same components: Controllers, Services, Mappers, Repository interfaces and/or data-access objects (DAOs), data-transfer-objects (DTO), and entity models. The business logic to implement CRUD operations for each domain is similar as well. Thus, many of these components can be abstracted with parameterized dependencies so that implementing them at the concrete level can be done with minimal lines of code. This abstraction is provided by the components submodule.

Although the components submodule greatly improves a developer’s ability to quickly implement Spring Boot components with full CRUD functionality, the developer experience requires implementing and/or extending the interfaces and abstract classes that have been parameterized. This task can be tedious and reduce developer productivity.

To improve the developer experience further by reducing the amount of code necessary to implement CRUD functionality for a domain’s Spring Boot components, the codegen project uses code generation technologies to auto implement the Controller, Service, Mapper, Repository interfaces and/or DAOs and DTOs based on a domain's Entity model. It is the Entity model that typically dictates differences in the business logic, after all.

Community Considerations

There are concerns about generating APIs based on introspecting database schemas (ruslantalpa r/java on Reddit), which is related to how this project introspects Entity models to generate code for Spring Boot components. Indeed, this coupling is an anti-pattern to the DTO pattern and could expose sensitive attributes.

These concerns are addressed with plans to support generating DTO objects based on the Entity model in ways that can exclude sensitive properties. There will also be support for providing DTO objects to the code generator thereby giving full granular control to the developer. Please see ##Next-Steps for more information.

Usage

Examples

IDE Configuration

Annotation with Entity

Development

Architecture

Abstracted Components

Annotation

Annotation Processing

At compile time, the project uses annotation processing to identify with Java Poet to write Java files compile time based on the components submodule that

Pitfalls

Next Steps

This codegen project is in alpha and currently under active development to provide additional and more robust features.

The roadmap can be viewed under the Codegen Project linked to this GitHub repository.

As such, the following features are not yet supported:

Nested Annotations

DTO

  • Implement DTO 1:1 based on Entity model
  • Ability to exclude attributes in the DTO that are present in the Entity model.

Business Logic for nested entities

Criteria Specification Search

Testing

GraphQL Support

Clone this wiki locally