Micronaut : The starting your first project

Micronaut : The starting your first project

Calling Super Heroes With Faker and Micronaut

Subscribe to my newsletter and never miss my upcoming articles

Micronaut is one of the java framework which can be used to create a Java microservices which are fast, reliable , modular and testable. You can also create serverless application with micronaut. If you have ever worked with Spring or any other java based web framework, you will find very less learning curve with it.

Source code : https://github.com/CODINGSAINT/super-heroes

Let's explore micronaut in this series.

Table of Content

Goal

The goal of this article is simple. Create a Hello world kind of application with micronaut. The application should build and start . We will also create a custom (GET) endpoint to see it working . Our endpoint will use Faker lib to fake to create fake superheros and return to us.

Prerequisite

We require

  • Java (11 or higer)
  • Maven /Gradle ( For this article we are using maven)

Approach

Creating sample project of micronaut

There are several ways to start the new project , you can use the launcher website or initializer using cmd I will be using launcher website for this article. Visit https://micronaut.io/launch/ . I am using maven as build tool and java 16 as jdk version. It depends on you if you want to use other tools and jdk version. I have also changed the application name as super-heroes and base package as com.codingsaint

Screenshot from 2021-08-19 12-09-16.png

Click on generate the project. It will download the zip version on your local maching. Unzip the file and open in folder / pom in your favorite IDE. I am using intelliJ for it.

Running the project

To run the project , you can simply open the terminal and type ./mvnw mn:run This will download the dependency and start the project on 8080 .

Screenshot from 2021-08-19 12-17-49.png

You can check localhost:8080

Adding dependency for java faker (Optional )

Java Faker lib will help us to create fake super hero names. The goal is to create 10 fake super heroes with their special power . When we hit /super-heros endpoint we should retrieve the our super heroes. Add the below dependency in pom file. The terminal in which you started the project will show logs of download dependency (if not present) . The application will be restarted for better developer experience.

<dependency>
      <groupId>com.github.javafaker</groupId>
      <artifactId>javafaker</artifactId>
      <version>1.0.2</version>
</dependency>

Creating a sample endpoint for heroes

Create a SuperHeroController.java class. Annotate it with @Controller ,and use faker to create fake super heroes for us. Below is the code for same

package com.codingsaint;

import com.github.javafaker.Faker;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Controller
public class SuperHeroController {

    @Get("super-heroes")
    public Collection<String> superheroes(){
        Faker fake = new Faker();
        List<String> superHeros= new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            var superHero=fake.superhero();
            superHeros.add(superHero.name()+" - "+superHero.power());
        }
        return superHeros;
    }
}

Hit localhost:8080/super-heroes , you will get super charged super heroes with their world saving powers as a response.

[
"General Storm Boy - Physical Anomaly",
"Magnificent Rachel Pirzad Eyes - Atmokinesis",
"Doctor Hawk Lord - Biokinesis",
"Magnificent Ink I - Toxikinesis",
"Arclight - Atmokinesis",
"Sobek - Empathy",
"Isis Dragon - Telekinesis",
"Cyborg Quantum - Levitation",
"Captain Hawk Man - Hypnokinesis",
"Mr Titan - Cloaking"
]

As Faker magic , you will generate new super heros every time you want.

Conclusion

It is super easy to create a simple REST project with micronaut. While there are other great frameworks who are equally good, micronaut seems exciting as well . It is now backed by oracle so we can be optimistic to see smooth progress in the framework as well.

If you like my articles you can support me by sharing the articles with your friends, twitter, facebook or any other social media. You can also buy me a coffee output-onlinepngtools.png

 
Share this