Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

''Hello, Micronaut'' Using Microstarter.io

DZone 's Guide to

''Hello, Micronaut'' Using Microstarter.io

Learn more about building a REST web service with Micronaut.

· Java Zone ·
Free Resource

For the past couples of months, I've worked to develop an online starter that helps developers generate Micronaut applications based on Micronaut-Profile Project templates. The Micronaut online starter was published on the Microstarter.io website. In this article, we will create a REST web service powered by the Micronaut framework using Microstarter.io. Let's get to it!

What Is Micronaut?

In a nutshell, Micronaut is a lightweight, JVM-based framework that's ahead of compilation time, with less startup time natively.

Microstarter.io

Visit https://www.microstarter.io. The current Microstarter.io landing page is a Micronaut application online starter. The online starter is very simple and descriptive by itself. It's pretty familiar to you if you are a user of another online starter framework.

Let's Get Started

We will create a REST web service using the Micronaut framework. We will use the "service" profile to create the REST web service. The "service" profile has all the dependencies for REST services. Therefore, we will not need to add extra dependencies or features.

Image title

So, let's start to fill in the project information:

  1. Profile: Select "service" as the Micronaut application type.
  2. Build: select "Gradle" as a build tool.
  3. Language: select "Java" as project language.
  4. Java Version: select 1.8.
  5. Group: Enter your group name. Ex: io.hashimati
  6. Artifact: Enter the artifact. Ex: HelloMicronaut. The artifact name will be the application name and service name. The group and artifact are the root package name of the service. In this tutorial, the root package of the project source code is io.hashimati.hellomicronaut.
  7. [Optional] Add "asciidoctor" feature.
  8. Click on the "Add Classes" button. The classes list should pop up on the button of the web page.
  9. Image titleAdd a Controller by following below steps:
    1. Select "Service" in the first combobox
    2. Select "Controller" from the second combobox.
    3. Put "HelloController" in the Name field.
    4. Put "rest" in the Package field.
    5. Enter "/hello" in Mapping Path field.
    6. Click Add button to add HelloController class.
    7. Press on "esc" button or click on the shaded area of the web page to close "Classes List".
  10. Check include gradle-wrapper.jar to include the build jar file.
  11. Click on "GENERATE A PROJECT" button or press "Alt+Enter" to generate the Gradle project and to download in zip file format.
  12. Extract the zip file.
  13. Open command line, go to the project directory. Then, run "gradlew run". This command will build and run the project. After the startup process is complete, open http://localhost:8080/hello/. The output of this service is code 200. So, you should see a blank web page in your web browser.

Image title

Now, we will create the "Hello, Micronaut!" REST service. Open the project with your favorite IDE. Then, open HelloController.java class. Import the @Produces and MediaType, which we will use them to specify the returned data type from the REST service. Obviously, our REST service will return the String payload.

import io.micronaut.http.annotation.Produces;
import io.micronaut.http.MediaType;


Add the HelloMicronaut() method to produce "Hello, Micronaut!".

package io.hashimati.hellomicronaut.rest;

import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.annotation.Produces;
import io.micronaut.http.MediaType;

@Controller("/hello")
public class HelloController {

    @Get("/")
    public HttpStatus index() {
        return HttpStatus.OK;
    }

    @Get("/micronaut")
    @Produces(MediaType.TEXT_PLAIN)
    public String helloMicronaut() {
        return "Hello, Micronaut!"; 
    }
}


From the command line, run "gradlew run" again to build and run the service. Next, open http://localhost:8080/hello/micronaut. Congratulations! You created your "Hello, Micronaut!" service.

Conclusion

Microstarter.io's Micronaut online starter is a simple tool that enables Java developers to create Micronaut application skeletons and packs all the required features and dependencies. We will continue to improve this website and develop many additional features in the future.

Happy coding!

Topics:
microservice ,cloud native ,java ,kotlin ,micronaut ,groovy ,java frameworks

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}