-
Notifications
You must be signed in to change notification settings - Fork 4
Codegen
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.
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.
As discussed in the Motivation section of this document, components have been abstracted to make implementing domain-driven APIs with Spring Boot easy with minimal amounts of new code. These components are contained in the submodule here.
Please note that only the search components are not supported at this time. See Next Steps.
In the Codegen submodule of this project, we take the level of abstraction even further by automatically generating the "minimal new code" that is required to implement at the concrete level of a specific domain. The abstracted components that are referenced for this generation are under the .
The @Harvest annotation here is what is used for the user to provide metadata that will be used to generate Spring Boot components at compile time.
@Target is a meta-annotation to scope which types of objects the annotation will be used for. The following table (source) illustrates different flavours of ElementType that defines these scopes.
| Element Type | Element to be Annotated |
|---|---|
| Type | Class, interface or enumeration |
| Field | Field |
| Method | Method |
| Constructor | Constructor |
| Local_variable | Local variable |
| Annotation_type | Annotation Type |
| Type_Parameter | Type Parameter |
| Parameter | Formal Parameter |
Because this annotation will be used on a class with an @Entity annotation, we have used the ElementType.TYPE.
The following article here describes the @Retention annotation.
@Retentionis also a meta-annotation that comes with some retention policies. These retention policies determine at which point an annotation is discarded. There are three types of retention policies: SOURCE, CLASS, and RUNTIME.
RetentionPolicy.SOURCE: The annotations annotated using the SOURCE retention policy are discarded at runtime.
RetentionPolicy.CLASS: The annotations annotated using the CLASS retention policy are recorded in the .class file but are discarded during runtime. CLASS is the default retention policy in Java.
RetentionPolicy.RUNTIME: The annotations annotated using the RUNTIME retention policy are retained during runtime and can be accessed in our program during runtime.
| Property Name | Description |
|---|---|
| domainContextPath | Defines the root directory that the generated components will be published in. |
| domainNameSingular | The singular form of the domain name. This value is used to generate directories and packages, component names, etc. |
| domainNamePlural | The plural form of the domain name. This value is used just like domainNameSingular, but is used instead of the singular form when the plural case is more appropriate grammatically. Appropriate cases that are used in code generation are when there are lists, collections, sets, etc., of a particular entity type. |
| parentDomainName | Used when an entity has an ownership association with another entity. This optional property ensures that the generated components follow the parent-child package structure this project uses. This property may not be relevant for some use cases and can be ignored. |
The following example below (pasted from here) shows how an AuthorEntity uses the @Harvest annotation.
package dev.springharvest.library.codegen.domains.authors.author.models.entities;
import dev.springharvest.codegen.annotations.Harvest;
import dev.springharvest.shared.domains.embeddables.traces.traceable.models.entities.AbstractTraceableEntity;
import jakarta.persistence.AttributeOverride;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotBlank;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@Slf4j
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
@Entity
@Table(name = "authors")
@AttributeOverride(name = "id", column = @Column(name = "id"))
@Harvest(domainContextPath = "/library", domainNameSingular = "Author", domainNamePlural = "Authors", parentDomainName = "Authors")
public class AuthorEntity extends AbstractTraceableEntity<UUID> {
@NotBlank
@Column(name = "name")
protected String name;
@Override
public boolean isEmpty() {
return super.isEmpty() && StringUtils.isBlank(name);
}
}HarvestBO below (pasted from here) is a business object (BO) model used to transmit metadata that is leveraged from the @Harvest annotation amongst the different factories we will use to generate each component layer of the Spring Boot api.
package dev.springharvest.codegen.models;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.TypeName;
import lombok.EqualsAndHashCode;
import lombok.Getter;
@Getter
@EqualsAndHashCode(callSuper = false)
public class HarvestBO {
private final String rootPackageName;
private final String domainNameSingular;
private final String domainNamePlural;
private final String parentDomainName;
private final String domainContextPath;
private final String crudRepositoryPackageName;
private final TypeName dtoTypeName;
private final String dtoPackageName;
private final String dtoName;
private final TypeName entityTypeName;
private final String entityName;
private final String entityPackageName;
private final String mapperPackageName;
private final String crudRepositoryName;
public HarvestBO(String rootPackageName, String domainNameSingular, String domainNamePlural, String parentDomainName, String domainContextPath) {
if (rootPackageName == null) {
throw new IllegalArgumentException("Root package name cannot be null");
}
if (domainNameSingular == null) {
throw new IllegalArgumentException("Domain name singular cannot be null");
}
if (domainNamePlural == null) {
throw new IllegalArgumentException("Domain name plural cannot be null");
}
if (parentDomainName == null) {
throw new IllegalArgumentException("Parent domain name cannot be null");
}
if (domainContextPath == null) {
throw new IllegalArgumentException("Domain context path cannot be null");
}
this.rootPackageName = rootPackageName;
this.domainNameSingular = domainNameSingular;
this.domainNamePlural = domainNamePlural;
this.parentDomainName = parentDomainName;
this.domainContextPath = domainContextPath;
// DTO
this.dtoPackageName = getPackageName("models.dtos");
this.dtoName = String.format("%sDTO", domainNameSingular);
this.dtoTypeName = ClassName.get(getDtoPackageName(), getDtoName());
// Entity
this.entityName = String.format("%sEntity", domainNameSingular);
this.entityPackageName = getPackageName("models.entities");
this.entityTypeName = ClassName.get(getEntityPackageName(), getEntityName());
// Mapper
this.mapperPackageName = getPackageName("mappers");
// Crud Repository
this.crudRepositoryPackageName = getPackageName("persistence");
this.crudRepositoryName = String.format("I%sCrudRepository_", domainNameSingular);
}
private String getPackageName(String subPackage) {
return String.format("%s.%s.%s.%s", rootPackageName, domainNamePlural.toLowerCase(), domainNameSingular.toLowerCase(), subPackage);
}
}The @Harvest annotation processing adapts a pattern similar to the hub-and-spoke design where the HarvestProcessor (the hub) uses one-to-many factories (spokes) to generate each layer of the Spring Boot API bottom-up, starting with the Repository layer:
Repository->Mappers->Services->Constants->Controllers
The following pseudocode illustrates how the @Harvest annotation is processed, and can be compared with the actual implementation here:
process(Set annotations, RoundEnvironment roundEnv):
Print a note that annotations are being processed
For each element annotated with @Harvest in the round environment:
If the element is not a class:
Call the error method to print an error message
Return false to exit processing
Extract the TypeMirror of the annotated class
Extract the properties from the @Harvest annotation
Extract the package name where the annotated class resides
Re-build the root package name using based on the extracted @Harvest property values
Generate the CRUD repository code using the CrudRepositoryGenerator
Generate the Mapper code using the MapperGenerator
Generate the CRUD service code using the CrudServiceGenerator
Generate the Controller Constants code using the ControllerConstantsGenerator
Generate the CRUD controller code using the CrudControllerGenerator
Return true to indicate successful processing
The code generators that create each Spring Boot component from the HarvestProcessor adopt a factory design pattern where the generate method handles the writing of each file based on the respective component it is specialized for. These generators use the Java Poet framework to make generating Java files easier to maintain.
The below pseudocode example demonstrates the logic for generating a Repository interface based on the domain that the @Harvest annotation is used.
generate(HarvestBO harvestBO, ProcessingEnvironment processingEnv):
// Prevent instantiation of the generator
Throw an UnsupportedOperationException with a private constructor message
Create an interface specification named harvestBO.getCrudRepositoryName():
Set the modifier to public
Add a superinterface of ParameterizedTypeName:
Use ICrudRepository from "dev.springharvest.crud.domains.base.persistence"
Parameterize with harvestBO's entity package and entity name, and "java.util.UUID"
Add an @Repository annotation from "org.springframework.stereotype"
// Build a Java file with the interface specification
Create a Java file with the package name from harvestBO for the CRUD repository and the interface specification
// Write the Java file to the file system
Use JavaPoetUtils to write the created Java file using the processing environment
The pseudocode above will create the following file based on the Author entity:
package dev.springharvest.library.codegen.domains.authors.author.persistence;
import dev.springharvest.crud.domains.base.persistence.ICrudRepository;
import dev.springharvest.library.codegen.domains.authors.author.models.entities.AuthorEntity;
import java.util.UUID;
import org.springframework.stereotype.Repository;
@Repository
public interface IAuthorCrudRepository_ extends ICrudRepository<AuthorEntity, UUID> {
}This generated IAuthorCrudRepository_ interface compares 1:1 to that a developer would manually implement, as shown here:
package dev.springharvest.library.domains.authors.persistence;
import dev.springharvest.crud.domains.base.persistence.ICrudRepository;
import dev.springharvest.library.domains.authors.models.entities.AuthorEntity;
import java.util.UUID;
import org.springframework.stereotype.Repository;
@Repository
public interface IAuthorCrudRepository extends ICrudRepository<AuthorEntity, UUID> {
}For brevity, the other generators will not be explained in this document, but they follow similar logic to the pseudocode above. Their respective source code can be referenced here, however.
Because annotation processing happens at compile time and JUnit tests are executed at runtime, a different approach needs to be taken to automate testing.
When booting the example application here, the user can navigate to the SwaggerUI page at http://localhost:8080/api/docs/ui, which uses the following OpenAPI specification at http://localhost:8080/api/docs, all from a single annotation. Try pasting the above json into the online SwaggerUI Editor here to see the OpenApi specification in a more user-friendly format.
{
"openapi":"3.0.1",
"info":{
"title":"OpenAPI definition",
"version":"v0"
},
"servers":[
{
"url":"http://localhost:8080/api",
"description":"Generated server url"
}
],
"paths":{
"/library/authors/create":{
"post":{
"tags":[
"Authors"
],
"summary":"Creates an entity.",
"description":"Use this API to create a new entity.",
"operationId":"create",
"requestBody":{
"description":"The DTO used to create the entity.",
"content":{
"application/json":{
"schema":{
"$ref":"#/components/schemas/AuthorDTO"
}
}
},
"required":true
},
"responses":{
"200":{
"description":"The entity with their generated primary key id.",
"content":{
"application/json":{
"schema":{
"$ref":"#/components/schemas/AuthorDTO"
}
}
}
}
}
}
},
"/library/authors/create/all":{
"post":{
"tags":[
"Authors"
],
"summary":"Creates entities.",
"description":"Use this API to create a new entity.",
"operationId":"createAll",
"requestBody":{
"description":"A list of DTOs used to create the entities.",
"content":{
"application/json":{
"schema":{
"type":"array",
"items":{
"$ref":"#/components/schemas/AuthorDTO"
}
}
}
},
"required":true
},
"responses":{
"200":{
"description":"The entities created with their generated primary key ids.",
"content":{
"application/json":{
"schema":{
"type":"array",
"items":{
"$ref":"#/components/schemas/AuthorDTO"
}
}
}
}
}
}
}
},
"/library/authors/update":{
"patch":{
"tags":[
"Authors"
],
"summary":"Updates an entity.",
"description":"Use this API to partially update a new entity.",
"operationId":"update",
"parameters":[
{
"name":"id",
"in":"query",
"required":true,
"schema":{
"type":"string",
"format":"uuid"
}
}
],
"requestBody":{
"description":"The DTO used to update its entity.\nOnly the attributes that are changed need to be passed into the dto.\nThe id in the DTO is ignored.\n",
"content":{
"application/json":{
"schema":{
"$ref":"#/components/schemas/AuthorDTO"
}
}
},
"required":true
},
"responses":{
"200":{
"description":"The updated entity.",
"content":{
"application/json":{
"schema":{
"$ref":"#/components/schemas/AuthorDTO"
}
}
}
}
},
"deprecated":true
}
},
"/library/authors/update/all":{
"patch":{
"tags":[
"Authors"
],
"summary":"Updates an entity.",
"description":"Use this API to partially update a new entity.",
"operationId":"update_1",
"requestBody":{
"description":"The DTO used to update its entity.\nOnly the attributes that are changed need to be passed into the dto.\nThe id in the DTO is immutable.\n",
"content":{
"application/json":{
"schema":{
"type":"array",
"items":{
"$ref":"#/components/schemas/AuthorDTO"
}
}
}
},
"required":true
},
"responses":{
"200":{
"description":"The entity with the given primary key id.",
"content":{
"application/json":{
"schema":{
"type":"array",
"items":{
"$ref":"#/components/schemas/AuthorDTO"
}
}
}
}
}
}
}
},
"/library/authors/find":{
"get":{
"tags":[
"Authors"
],
"summary":"Retrieves the entity.",
"description":"Use this API to retrieve an entity by their primary key id.",
"operationId":"findById",
"parameters":[
{
"name":"id",
"in":"query",
"description":"The primary key id of the entity to be retrieved.",
"required":true,
"schema":{
"type":"string",
"format":"uuid"
}
}
],
"responses":{
"200":{
"description":"The entity with the given primary key id",
"content":{
"application/json":{
"schema":{
"$ref":"#/components/schemas/AuthorDTO"
}
}
}
}
}
}
},
"/library/authors/find/all":{
"get":{
"tags":[
"Authors"
],
"summary":"Retrieves a list of entities.",
"description":"Use this API to retrieve an entity by their primary key id. Leaving all parameters empty will return all entities.",
"operationId":"findAll",
"parameters":[
{
"name":"pageNumber",
"in":"query",
"description":"The page number of the pageable entities that are returned.",
"required":false,
"schema":{
"type":"integer",
"format":"int32"
}
},
{
"name":"pageSize",
"in":"query",
"description":"The maximum number of entities to return on any single page.",
"required":false,
"schema":{
"type":"integer",
"format":"int32"
}
},
{
"name":"sorts",
"in":"query",
"description":"The attributes to sort by, ascending or descending.",
"required":false,
"schema":{
"type":"array",
"items":{
"type":"string"
}
},
"example":"id-asc"
}
],
"responses":{
"200":{
"description":"The retrieved entities.",
"content":{
"application/json":{
"schema":{
"$ref":"#/components/schemas/PageAuthorDTO"
}
}
}
}
}
}
},
"/library/authors/count":{
"get":{
"tags":[
"Authors"
],
"summary":"Counts the entities.",
"description":"Use this API to retrieve an number of entities in its domain.",
"operationId":"count",
"responses":{
"200":{
"description":"The number of entities in its domain.",
"content":{
"application/json":{
"schema":{
"type":"integer",
"format":"int64"
}
}
}
}
}
}
},
"/library/authors/delete":{
"delete":{
"tags":[
"Authors"
],
"summary":"Delete an entity by its id.",
"description":"Use this API to delete an entity via its id.",
"operationId":"deleteById",
"parameters":[
{
"name":"id",
"in":"query",
"description":"The id used to delete the entity by.",
"required":true,
"schema":{
"type":"string",
"format":"uuid"
}
}
],
"responses":{
"204":{
"description":"The entity was deleted by its id successfully. A redirect is not required."
}
}
}
},
"/library/authors/delete/all":{
"delete":{
"tags":[
"Authors"
],
"summary":"Delete all entities by their ids.",
"description":"Use this API to delete any entity in this domain that matches the passed ids.",
"operationId":"deleteAllById",
"requestBody":{
"description":"The ids used to delete the entities by.",
"content":{
"application/json":{
"schema":{
"type":"array",
"items":{
"type":"string",
"format":"uuid"
}
}
}
},
"required":true
},
"responses":{
"204":{
"description":"The entity was deleted by its id successfully. A redirect is not required."
}
}
}
}
},
"components":{
"schemas":{
"AbstractTraceUsersDTOUUID":{
"type":"object",
"properties":{
"createdBy":{
"type":"string",
"format":"uuid"
},
"updatedBy":{
"type":"string",
"format":"uuid"
}
}
},
"AuthorDTO":{
"type":"object",
"properties":{
"id":{
"type":"string",
"description":"The id of the author.",
"format":"uuid",
"example":"00000000-0000-0000-0000-000000000001"
},
"traceData":{
"$ref":"#/components/schemas/TraceDataDTOUUID"
},
"name":{
"type":"string",
"description":"The name of the author.",
"example":"Dr. Seuss"
}
},
"description":"A book's author."
},
"TraceDataDTOUUID":{
"type":"object",
"properties":{
"traceDates":{
"$ref":"#/components/schemas/TraceDatesDTO"
},
"traceUsers":{
"$ref":"#/components/schemas/AbstractTraceUsersDTOUUID"
}
}
},
"TraceDatesDTO":{
"type":"object",
"properties":{
"dateCreated":{
"type":"string",
"format":"date-time"
},
"dateUpdated":{
"type":"string",
"format":"date-time"
}
}
},
"PageAuthorDTO":{
"type":"object",
"properties":{
"totalElements":{
"type":"integer",
"format":"int64"
},
"totalPages":{
"type":"integer",
"format":"int32"
},
"size":{
"type":"integer",
"format":"int32"
},
"content":{
"type":"array",
"items":{
"$ref":"#/components/schemas/AuthorDTO"
}
},
"number":{
"type":"integer",
"format":"int32"
},
"sort":{
"type":"array",
"items":{
"$ref":"#/components/schemas/SortObject"
}
},
"numberOfElements":{
"type":"integer",
"format":"int32"
},
"pageable":{
"$ref":"#/components/schemas/PageableObject"
},
"first":{
"type":"boolean"
},
"last":{
"type":"boolean"
},
"empty":{
"type":"boolean"
}
}
},
"PageableObject":{
"type":"object",
"properties":{
"offset":{
"type":"integer",
"format":"int64"
},
"sort":{
"type":"array",
"items":{
"$ref":"#/components/schemas/SortObject"
}
},
"paged":{
"type":"boolean"
},
"unpaged":{
"type":"boolean"
},
"pageNumber":{
"type":"integer",
"format":"int32"
},
"pageSize":{
"type":"integer",
"format":"int32"
}
}
},
"SortObject":{
"type":"object",
"properties":{
"direction":{
"type":"string"
},
"nullHandling":{
"type":"string"
},
"ascending":{
"type":"boolean"
},
"property":{
"type":"string"
},
"ignoreCase":{
"type":"boolean"
}
}
}
}
}
}Complete the Setup guide.
Ingest the dev.springharvest.codegen dependency with the desired version into your preferred build tool.
Maven
<dependency>
<groupId>dev.springharvest</groupId>
<artifactId>codegen</artifactId>
<version>${version}</version>
</dependency>Gradle
compileOnly "dev.springharvest:codegen:${version}"
annotationProcessor "dev.springharvest:codegen:${version}"
testAnnotationProcessor "dev.springharvest:codegen:${version}"
compileOnly files("$buildDir/generated/sources/annotationProcessor/java/main")
annotationProcessor files("$buildDir/generated/sources/annotationProcessor/java/main")Annotate a domain's Entity model with the @Harvest annotation and set the applicable values for domainContextPath, domainNameSingular, domainNamePlural, and parentDomainName.
See Development documentation for more information.
The Entity being annotated with @Harvest must already have the @Entity annotation.
The books-catalogue-codegen example can be referenced to view how the annotation is used.
Please note that since this example is a nested submodule for the project repository, its build.gradle file consumes adjacent submodules in the project as opposed to consuming the dependencies from GitHub. As such, the build.gradle configuration shown in this example may need changes to be used in your own project. Please reference the Setup guide for more information.
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.
- Implement DTO 1:1 based on Entity model
- Ability to exclude attributes in the DTO that are present in the Entity model.