Creating a Rest application with Micronaut

Daniel Dias
Daniel Dias
Published in
4 min readApr 22, 2020
https://micronaut.io/

This post we go create a simple Rest application with Micronaut.

For this post we will create 2 simple endpoints (get/post) using the following libs and resources:

Please, enjoy the subject. And sorry for the grammatical mistakes.

Introduction at Micronaut

Micronaut is a framework very similar in Spring for the construction of microservices.

Micronaut provide tools necessary to work fine in construction of applications :

  • Dependency Injection and Inversion of Control (IoC)
  • Sensible Defaults and Auto-Configuration
  • Configuration and Configuration Sharing
  • Service Discovery
  • HTTP Routing
  • HTTP Client with client-side load-balancing

According to the site, Micronaut o avoids disadvantages in other frameworks like Spring, Spring Boot, and Grails by providing:

  • Fast startup time
  • Reduced memory footprint
  • Minimal use of reflection
  • Minimal use of proxies
  • Easy unit testing

From the little I played with the Micronaut, I found it very interesting, easy and really the way of programming is similar to Spring, that is, your experience in Spring is reused. : )

Let’s go at code : )

Creating your Maven Project

First, create a new project maven with the following dependencies :

Here are the basic dependencies for the Micronaut working:

Basically, that’s all for the Micronaut to work well : )

Here too add dependencies to Bean Validation, Micronaut-Data, H2 and Connection Poll JDBC TomCat

Creating the Classes

Now we will create a class Model with name Person, very simple with annotation de JPA and Bean Validation :

Here is a simple class with annotations of JPA and Bean Validation, more with a unique difference, the class contains an annotation of Micronaut that is “@Introspected”. She is necessary to instantiate and read/write a bean property without using reflection or caching reflective metadata.

The use of Bean Validation here is limited, if you can an Full Bean Validation, please add the dependency:

The next step is to create a class Repository for starting the use of Micronaut-Data :

So this is a simple interface that extends the interface CrudRepository.

Micronaut-Data is very similar to the Spring-Data and Apache DeltaSpike. Very friendly to the developer use.

Micronaut-data provides other interfaces as shown in the image:

https://micronaut-projects.github.io/micronaut-data/latest/guide/index.html#repositories

Now we will create a class Controller with name PersonController and with the following Endpoints:

Here is only a simple Controller class with two endpoint GET/POST.

So, in line 19 we do use of annotation “@Validated” in your Controller to enable the support at validation in our POJOs Class.

In line 20 is annotation common in other framework MVC (Spring, Eclipse Krazo), here he receive an URI and by default it is already enabled to produce and consume JSON.

In line 24 and 25, we use the JSR-330 annotation @Inject and then declare the “PersonRepository” and inject it.

Next line(27) is the our first endpoint that is POST, here not declared a URI/path to her, more is possible to add resources how to example :

In method savePerson(), we have an Object Person and two annotations how parameter.

The annotation “@Body” to indicate to Micronaut the parameter which will receive the data and the annotation “@Valid” use is required to validate our Person bean, which must be used in conjunction with the “@Validated” annotation.

In the next line (29), we call our repository and call the method save and a person is included in database H2 and after return an “HttpResponse” with a Status Code and an Object in your body this a Message Class that receives two parameters.

Then we have our endpoint that responds to a GET, whose function is to retrieve all data from the database, calling the findAll method of the Micronaut-Data.

to finish we created a main class and we also created a file called application.yml with JPA and H2 properties for the persistence function.

So the Main Class is only this :

and in folder “resources” maven create the file application.yml :

Now run the Main Class and open your postman and test our application : )

that’s all for today, I hope you enjoyed it.

I do not work with this framework, but I found it very easy to move and I believe it is a good alternative to Spring, since it is similar, in addition the Micronaut documentation is very complete and easy to understand.

Code: https://github.com/Daniel-Dos/danieldiasjava-medium-english/tree/master/Creating-a-Rest-application-with-Micronaut

--

--

Daniel Dias
Daniel Dias

SouJava Board Member, JCP Member, JSR-371 (MVC 1.0), JSR-382 (Config) specifications contributor, EG JSR-385 (UoM) and Eclipse Committer .