Micronaut: Rapid Development With MicrostarterCLI
MicrostarterCLI is a rapid development tool for Micronaut applications. In this article, we will demonstrate how it makes developing GraphQL services easy.
Join the DZone community and get the full member experience.
Join For FreeMicrostarterCLI is a rapid development tool for Micronaut applications. It helps you as a developer to generate standard reusable codes, configurations, or patterns as you need in your application.
Application Description
In this article, we will develop an ArabicNames service that has REST and GraphQL endpoints. To begin with the development, we will generate a Micronaut application project from Micronaut Launch. Then, we will use MicrostarterCLI to develop the following:
- ArabicName entity class.
- JDBC repository and Service classes for the Arabic Entity Class.
- REST and GraphQL endpoints to consume the Arabic Name services.
- Liquibase migration file.
- TestClass to test Arabic Name REST endpoints.
ArabicName Entity Attributes
Attribute
|
Data Type | description |
---|---|---|
letter | String | English letter |
name | String | The name in English |
nativeArabic | String | The name is Arabic |
meaning | String | The name's meaning in English |
Let's Do It
Step 1: Visit https://micronaut.io/launch and generate a Micronaut application with a language of your choice. You can add the features you need for the Arabic Names service, but MicrostarterCLI will add all the required features if you don't add them from Micronaut Launch.
In this article, I'll generate the project as follows:
Micronaut Application
|
|
---|---|
Application Type
|
Micronaut Application
|
Java Version
|
11
|
Name
|
ArabicNames
|
package
|
io.hashimati.dzone
|
Micronaut Version
|
Latest
|
language
|
Java
|
Build Tool
|
Gradle
|
Test Framework
|
JUnit
|
Step 2: Download the MicrostarterCLI release zip file. Then, unzip it and copy the mc.jar
, mc.bat
, and mc
files to the root of the project's folder.
Step 3: Go to the project directory from the terminal/command prompt and run the "configure
" command:
mc configure
The "configure
" command will take you through a step-by-step process to add the project's dependencies with their necessary configuration and the database information. The dependencies include:
- Port Number: The default port value is 8080
- Reactive Framework: Reactor, RxJava2, or RxJava3
- Database Type Dependencies: MongoDB, H2, MySQL, Oracle, MariaDB, Postgres, or SQL Server
- Database Access Framework: Micronaut Data, ReactiveMongo, or Gorm
- Database Migration Tool: Liquibase or Flyway
- Messaging: Kafka, RabbitMQ, NATs.io, or GCP-PubSub
- Caching: Caffeine
- Micrometers
- Tracing: Jaeger or Zipkin
- GraphQL: GraphQL Java Kickstart
- OpenAPI: MicrostarterCLI will add this dependency by default.
We will do the configuration as follows:
Step 4: Run the "entity
" command to generate the ArabicName's entity, repository, service, controller, test-controller, client, GraphQL files, and Liquibase files.
mc entity -e ArabicName --graphql
When the command starts, it will ask you to enter the collection's name or the table. Then, it will prompt you to enter the attributes. Add the attributes as the following table:
Attribute | Type | Validation | FindBy() Method | FindAllBy() Method | UpdateBy() Method |
Name | String | - | Yes | No | Yes |
letter | String | - | No | Yes | No |
NativeArabic | String | - | Yes | No | No |
Meaning | String | - | No | No | No |
Step 5: Open the project with your favorite IDE and check the generated files.
Step 6: Run the "gradlew test
" command to run a JUnit test for the ArabicName REST endpoints.
gradlew test
Step 7: Run the "gradlew run
" command to run the application.
Step 8: Try the REST services using OpenAPI. The OpenAPI is accessible on this URL: http://localhost:8080/swagger/views/swagger-ui/index.html.
We will save the below object by calling "http://localhost:8080/api
{
"name": "Abbas",
"letter": "A",
"nativeArabic": "عباس",
"meaning": "Another name for a lion. The lion that the lions flee from"
}
Step 9: Try the GraphQL services. Open "http://localhost:8080/graphiql". Retrieve all Arabic Names objects by calling the below query.
query{
findAllArabicName{
name,
letter,
nativeArabic,
meaning
}
}
Conclusion
MicrostarterCLI is a progressive rapid development tool, and it has a lot of features that cut development time and remove the burden of the configuration. I'll try to cover the MicrostarterCLI features in more depth in subsequent articles. The source code for this project is available here.
Happy coding!
Opinions expressed by DZone contributors are their own.
Comments