3

Micronaut: Rapid Development With MicrostarterCLI

 2 years ago
source link: https://dzone.com/articles/developing-micronaut-application-with-microcli
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

MicrostarterCLI 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: 

  1. ArabicName entity class.
  2. JDBC repository and Service classes for the Arabic Entity Class. 
  3. REST and GraphQL endpoints to consume the Arabic Name services.
  4. Liquibase migration file.
  5. 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
ArabicNames
package
io.hashimati.dzone
Micronaut Version
Latest
language
Build Tool
Gradle
Test Framework
JUnit

15794055-screenshot-2022-04-08-152333.png

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. 

15794063-microstartercli.png

https://github.com/hashimati/MicrostarterCli/releases/tag/v0.1.1

Step 3: Go to the project directory from the terminal/command prompt and run the "configure" command: 

Shell
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:

15794071-configure.png

Step 4: Run the "entity" command to generate the ArabicName's entity, repository, service, controller, test-controller, client, GraphQL files, and Liquibase files.

Shell
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

15794076-createentity.png

Step 5: Open the project with your favorite IDE and check the generated files.

15794122-id.png

Step 6: Run the "gradlew test" command to run a JUnit test for the ArabicName REST endpoints. 

Shell
gradlew test

15794093-test.png

Step 7: Run the "gradlew run" command to run the application.  

15794098-run-project.png

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/v1/arabicName/save".

{
	"name": "Abbas",
	"letter": "A",
	"nativeArabic": "عباس",
	"meaning": "Another name for a lion. The lion that the lions flee from"
}

15794104-swagger.png

Step 9: Try the GraphQL services. Open "http://localhost:8080/graphiql". Retrieve all Arabic Names objects by calling the below query.

Markdown
query{
  findAllArabicName{
    name, 
    letter, 
    nativeArabic, 
    meaning
  }
}

15794112-graphql.png

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!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK