Groovy CRUD Web App Using Micronaut

Reading Time: 3 minutes

1. About

1.1 Introduction

Micronaut is an open-source JVM-based software framework for building lightweight, modular applications and microservices. It helps to create microservices with small memory footprints and quick startup time.

2. Use Case

Let’s take a use case. We are going to create a web app with the help of Groovy and Micronaut to perform CRUD operations. For that, we have to implement the CRUD web app so that it will have all the entries of flight records. We can Add, Update, Delete and Read all the records.

3. Pre-Requisite

You need to make sure that the following components are already installed in the system:

  1. JDK 1.8 or greater installed with JAVA_HOME.
  2. Groovy runtime.
  3. Micronaut.
  4. Gradle 3.6.1

4. Development

To create a Micronaut project just navigate to https://micronaut.io/launch/ and create your starter project.

create-project

The project structure will be as follows-

project-structure

Open the build.gradle file and add the following dependencies :

// HANA DB driver.
com.sap.cloud.db.jdbc:ngdbc:2.13.9 

// To access the VCAP variable of the app. This will be needed to read the public key using which the JWT token will be verified.
io.pivotal.cfenv:java-cfenv-boot:2.4.0 

// To verify the JWT token using the public key certificate extracted using the above dependency.
com.sap.cloud.security:sapjwt:1.5.27.5 

// To extract the user details and the roles from the JWT token.
com.sap.cloud.security:java-security:2.13.0 

Create a FlightEntity class and annotate with @MappedEntity(‘SFLIGHT’)

Add entity feilds flightId, flightName, flightFrom, flightTo and flightDate where flightId field will be unique.
Annotate flightId with @Id and @Column(name = 'ID') and rest of field will be annotated with @Column

Create FlightRepository Repo class

Create FlightRepository Repo class and extends CrudRepository

Create FlightController class

//Add sFlightRepository bean of FlightRepository class
//Add controller method which will take FlightRepository object as a  parameter
@Inject
    FlightController(FlightRepository sFlightRepository) {
        this.sFlightRepository = sFlightRepository

        //Add some records in the table, if the table empty.
        if (this.sFlightRepository.findAll().isEmpty()) {
            this.sFlightRepository.save(new FlightEntity(flightId: 1,
                    flightName: 'IndiGo',
                    flightFrom: 'Mumbai',
                    flightTo: 'Chennai',
                    flightDate: new Date()))
            this.sFlightRepository.save(new FlightEntity(flightId: 2,
                    flightFrom: 'New Delhi',
                    flightName: 'Air India',
                    flightTo: 'Mumbai',
                    flightDate: new Date()))
            this.sFlightRepository.save(new FlightEntity(flightId: 3,
                    flightName: 'TruJet',
                    flightFrom: 'Agra',
                    flightTo: 'Delhi',
                    flightDate: new Date()))
            this.sFlightRepository.save(new FlightEntity(flightId: 4,
                    flightName: 'SpiceJet',
                    flightFrom: 'Delhi',
                    flightTo: 'Pune',
                    flightDate: new Date()))
        }
    }

// Fetch all records
Add getAll(@Nullable Principal principal)
 
// Update particulat record of table
Add update(@Body FlightEntity sFlightEntity, @PathVariable('id') String id)

// Delete particulat record of table 
delete(@Body FlightEntity sFlightEntity, @PathVariable('id') String id)
 
// This is for admin, he can perform the operations.
operation(@Nullable @PathVariable('name') String name)

Create Main Application class

6. Work Flow

Run Application.java class

main-app

You can navigate to http://localhost:8080 , You can see the FLIGHT table is generated and is also having entries.

main-data

To add a record in a table just click on Create button

update-record

To update a record in a table just click on the update action button

update-record

To delete a record in a table just click on the delete action button

delete-record

We have performed all the CRUD operations.

6. Conclusion

In this blog, we have covered the Groovy CRUD web app with the help of the Micronaut framework. Now you are ready with the understanding of the Groovy CRUD web app with the help of the Micronaut framework. For more, you can refer to the documentation: https://github.com/knoldus/groovy-crud-web-app-with-micronaut

7. References

https://guides.micronaut.io/latest/micronaut-microservices-distributed-tracing-jaeger-gradle-java.html

Written by 

Gaurav Dubey is a Java Developer at Knoldus Software LLP. He has done M.CA from Kurukshetra University and completed Bachelor's Degree in Computer Science from M. D. University. He is a tech enthusiast with good knowledge of Java. He is majorly focused in Java practice. On the personal front, he loves to cook and travel.

Leave a Reply