DEV Community

Rafael Borrego for SanExperts

Posted on • Updated on

How to create an API generator

Development teams are creating dozens of APIs per project since the arrival of microservices and we should try to make as easy as possible for them to create new ones, reduce repetitive tasks and standardise different patterns and configurations so they are applied the same way everywhere.

Do you want to learn how to create a tool that allows to generate new APIs within seconds and achieve the above goals? If so we are going to show you how to do it with Maven archetypes, a tool that allows to generate any type of project, even non-Java ones.

What can be automated?
I guess your pipeline, Docker and configuration files are quite similar, they just change some Git urls and project name.

What about the actual code?
I guess in most projects you do the same type of actions (create, read, modify and delete) and the main difference is the type (loan, mortgage, trade...) and they follow a similar structure (endpoints on the top layer, business logic in the services, queries on the repositories, and so on).

What about the validations and tests?
I guess you have special ones focused on specific scenarios but there are also some generic ones that feel repetitive to implement them again. All of them can be automated so developers don't spend hours writing them again or copying and pasting from previous projects. They can spend their valuable time focused on writing specific business rules (such as ensuring shops cannot sell more products than what they have on stock) or building some amazing features none of your competitors has yet.

How to do it?

There are a few options:
1) Create an API generation pipeline: this is a great idea and can be done using e.g. Ansible scripts. The problem is that these scripts are usually managed by a centralised team like SRE that may not have capacity to generate projects that suit the needs of every team, not to mention that each team may use completely different technologies (Java vs Kotlin, Angular vs React, ... ).
2) Create your own Spring Initializr: Pivotal have created a tool to create APIs and we can customise it, but it is focused on Spring Boot projects and we may want to provide generators for also other technologies.
3) Create your own archetypes: Maven allows to create projects from a template providing some input parameters. We will show how to create one and use it.

Steps to create your first archetype:
1) Choose a good project to use as a reference. You can modify it later but it adds extra work to re-implement one or change its structure, rename package paths, etc.
2) Create an archetype: you just have to run the command mvn archetype:generate and choose the base template you prefer.
3) Create a configuration file in which you specify which folders should be picked and which files inside them. You can find an example here.
4) Copy the code of your reference project inside the archetype-resources folder.
5) Replace the variable parts (like the entity names) by placeholders. The syntax for the folder names is __text-to-replace__ and inside the code it is ${domain}. You may want to do some smarter replacements what can be done using Velocity templates.
6) Add it to your Maven repository, either to your local one (using mvn install) or to your remote one (using mvn deploy, ideally in a pipeline).

That's it. Now you will want to create your own API using the archetype. You just have to run a command in which you provide its basic details (Maven groupId, artifactId and version) and the values to replace on the placeholders, and it will be generated within seconds.

An example would be:
mvn archetype:generate -DarchetypeGroupId=com.mycompany -DarchetypeArtifactId=my-first-archetype -DarchetypeVersion=1.0.0-SNAPSHOT -entity=customer

Do you want to see the full code of an archetype? You can find one here.
Do you want to share other ways to automate your project creation or want to ask how to automate other tasks? If so we would love to read your comments!

Top comments (0)