Scroll Top

Micronaut setup and getting started with Sample CRUD application

In my previous blog, we have discussed the new Microservices framework i.e Micronaut.

Do check that blog for the introduction and to know the advantages of the Micronaut.

As part of this blog, I would like to take you through the installation and implementation of a simple business use case with Micronaut framework, so you can immediately get started with the build, deploy and run the simple application.

Prerequisites

Install Micronaut on windows

As part of this article, I will take you through the Micronaut installation on Windows 10.

First, we need to install the Micronaut CLI on windows

  1. Download the latest Micronaut CLI binary from the Micronaut download page
  2. Unzip the downloaded binary file into your system
  3. Create the MICRONAUT_HOME environment variable with the installation directory path.
  4. Update your windows PATH environment variable, by appending MICRONAUT_HOME variable as well:  %MICRONAUT_HOME%\bin

After the environment variable setup, we should be able to run the Micronaut CLI from the command prompt:

Now we are all set for the server application creation in the Micronaut.

Use case

I took a simple use case from  HR Management domain i.e. Creation of an employee, Read employee details, Updating salary and Deleting user to create a sample application,  using the Micronaut framework.

Creating a Server Application

By using the Micronaut CLI, we can easily create a new server application. As part of the demonstration of The Micronaut server application, I will be creating a simple CRUD application. We can create a new Micronaut application using the CLI in Java, Groovy or Kotlin, if we don’t specify any language while creating the application then it considers the “Java” by default. We can also specify the build option i.e Maven or Gradle while creation of the application if we don’t specify any build option then “Gradle” will be taken by default.

The following command will be used to create the Micronaut application in Java with Maven build:

mn create-app HRManagement –build maven

The Micronaut HTTP server will run on a random port by default but we can update the default configurations to run on the standard port in the following file: src/main/resources/application.yml

Just uncomment the server and port in application.yml file

Import above Application in Eclipse and select the Automatically configure JDT APT in to support annotations:

Following is the Micronaut project structure after importing into Eclipse:

Now create a controller to define the endpoints for CRUD operations. I have created the EmployeeController to perform the CRUD operations on the Employee object.

Following is the Employee Controller java file:

Following is the Service implementation file which has the actual implementation of Employee CRUD operations:

Test the server application from Postman

Run the above Micronaut application and send the requests to each endpoint sequentially.

Add a new Employee:

The employee created successfully:

Get Employee details:

Get Employee service returned with complete details of Employee, we could define more fields in Employee table to store but in this example, I just added Salary along with Employee Id and Name.

Update Employee Salary through updateEmployee service:

Employee salary has been updated successfully:

Verify the above Employee details in the table to make sure that we have proper data before deletion of the above Employee:

Delete the Employee by using the deleteEmployee Service:

The employee has been deleted successfully:

Verify the data in the table as well, if no data is available in table, services are working fine:

Summary :

As a part of this article, we have covered Micronaut  CLI installation on windows, learned and prepared a sample Micronaut application with CRUD operations and demonstrated the services. 

I hope this article was informative and leaves you with a better understanding of The Micronaut framework. At Walking Tree, we are excited about the possibilities that Microservices ecosystem brings in. Stay tuned for more articles on this topic.

Related Posts

Comments (5)

Things are not clear, where is employee dao class. i dont understand how object is going in EmployeeDao and saving there. code gives idea about implementation logic but not real implementation of saving or updating in database. if there is git repository of this project then please share it

Yes, a git repo will be useful!

1. Think you are missing so much from this document, what version of eclipse ?
Current version downloaded appeared to have all those things as part of it

2. How did you import it ?
> Following is the Micronaut project structure after importing into Eclipse:
– This doesn’t really delve into how you went about getting the thing to work under windows

I get that the DAO, model, and service is up for interpretation depending upon how one would like to execute upon it, but do at least provide a warning next time for those that are very new to the concept of CRUD, dependency injection, etc.

Things are missing and not clear at some point. As a new bei to the micronaut, I want be able to understand where actually the MySQL database connectivity has been done. and what actually DAO. So proper explanation need to know more about this.

Leave a comment

Privacy Preferences
When you visit our website, it may store information through your browser from specific services, usually in form of cookies. Here you can change your privacy preferences. Please note that blocking some types of cookies may impact your experience on our website and the services we offer.