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

JetBrains IntelliJ

Please follow the documentation for Configuring an annotation profile in IntelliJ, ensuring that the Enable annotation processing setting is enabled.

Eclipse Please follow the documentation for Eclipse JDT-APT Project here.
VSCode Please follow the documentation for Annotation processing support in Gradle for VSCode.

Annotation with Entity

Development

Architecture

Abstracted Components

Annotation

Annotation Processing

Pitfalls

Next Steps

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

As such, the following features below are not yet supported but are planned for future development.

For more information about these and other planned features, please visit the backlog under the Codegen Project linked to this GitHub repository.

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

IDE Agnostic Annotation Processing

Resources

Clone this wiki locally