Community IntelliJ IDEA Java Tips & Tricks

Webinar Summary: Developing Micronaut applications with IntelliJ IDEA

On September 18, 2020, we hosted a live webinar on ‘Developing Micronaut Applications Using IntelliJ IDEA’ by Iván López, Senior Software Engineer at Object Computing. This session included a brief overview of the Micronaut framework with lots of live examples.

IntelliJ IDEA provides coding assistance specific to the Micronaut framework. For example, you can autocomplete Ctrl+Space parameters in application.properties and application.yml files, see quick documentation Ctrl+Q, and navigate to their definitions by holding down the Ctrl key. There is also integration with the Bean Validation and Endpoints tool windows.

Session details

Before diving into the live demos, Iván shared an overview of the Micronaut framework. He mentioned that Micronaut is not just for creating Microservices, but also for serverless applications. An ultra lightweight JVM framework, which uses memory very efficiently, Micronaut is a reactive framework based on Netty, and it works with Java, Groovy, and Kotlin.

Since the core team working on Micronaut has a long background of working with Spring, Iván mentioned that Spring and Spring Boot developers will feel right at home when working with Micronaut in terms of the concepts, injections, controllers, repositories, and other aspects.

He then went into detail about the feature that sets Micronaut apart from other frameworks, that is, Ahead of Time compilation. Traditional annotation-based JVM frameworks like Spring or MicroProfile analyze applications at runtime (for controllers, services, auto-configuration, and others) and cache all this information in the memory. But Micronaut generates classes when developers compile their applications, which includes the meta-information required for Micronaut to understand their application. No memory caching is required by Micronaut. Micronaut doesn’t need to use either reflection or runtime proxies, dynamic class loading, or dynamic byte code generation. This results in faster startup and a very low memory footprint.

Iván shared that developers can use Micronaut to build Cloud-native and serverless applications and it supports it with multiple features, like, Self-discovery, 12-factor apps, externalized configuration, build-in HTTP Client, distributed tracing, and security. Micronaut can also be used to create CLI (Command Line Interface) applications, gRPC applications, and Messaging applications. Micronaut Data is a lightweight layer to access the database and execute queries. Micronaut supports GraavlVM out of the box.

Micronaut 1.0 was released in October 2018, and since then Micronaut has had more than 35 releases. The latest stable Micronaut release is 2.0.2.

Iván showed how to bootstrap a Micronaut application using CLI, launch.micronaut.io, and IntelliJ IDEA’s project wizard. IntelliJ IDEA users can use their favorite features or shortcuts while using the IntelliJ IDEA project wizard to bootstrap their Micronaut application, like using search to find the features they want to add to their applications.

Iván then demonstrated how to bootstrap a "Hello World" Micronaut application using multiple features like Micronaut Data JDBC, Flyway Database Migration, Hikari JDBC Connection Pool, MySQL Open source database, Testcontainers, and Micronaut Management. After IntelliJ IDEA created the project files, he executed the test classes.

Then he added a ‘HelloWorld’ controller and service to the application, suggesting constructor injection for services in the controller rather than the option of field injection. The former helps make fields final.

Developers usually switch from their IDE to an internet browser window to test their web applications. This isn’t required in IntelliJ IDEA. Iván demonstrated the text-based integrated HTTP Client in IntelliJ IDEA that can test requests to an application’s public endpoints from within the IDE, by clicking on the icon in the gutter area next to the HTTP Controller.

Iván then demonstrated how to define HTTP requests as text and run them using the same HTTP Client window in IntelliJ IDEA. Since the window is part of the IDE, you can use the same shortcuts like duplicating lines of text and moving them around. You can also use this window to convert from cURL to HTTP requests. If you are unsure how to type in these values, you can click on the ‘examples’ tab on the top right to get examples of GET or POST requests, requests with authorization, and requests with tests.

Iván also demonstrated working with Micronaut Management – showing info, beans, health, and other information for your endpoints.

He was kind enough to show us a real-life project to demonstrate how IntelliJ IDEA helps with the configuration in YAML files. He demonstrated how to use Gradle to compile Java tasks so that changes to the configuration files are picked up.

Iván also demonstrated how to use the HTTP Client window in IntelliJ IDEA to view endpoints and also navigate to the source code by clicking on its name. Continuing his live demo, Iván then showed how to work with events in Micronaut applications. It was interesting to see how he demonstrated the use of the various gutter icons available for the Micronaut framework in IntelliJ IDEA, like events, controller, navigate to event publisher, navigate to event listeners, scheduled method, and factory bean.

Additional resources

You can get a copy of Iván’s slides here.

Here’s the application Iván mentioned during his talk.

You can learn more about getting started with developing Micronuat applications from the following links:

Follow-up Q & A

Though Iván answered all these questions during the webinar, we are also including them here because they were important questions:

I am exploring Micronaut. I couldn’t find a way to do REST exposed from repositories as Spring DATA. Is there any feature like that or one coming in the future?

Iván: It is possible to inject a Micronaut Data repository in a controller and expose it, but that is considered bad practice. In my opinion, you should create a service that uses the repository and is injected in the controller. Also use a DTO instead of exposing your domain entity directly.

Any plans to support Scala out of the box in the way Groovy, Kotlin, and Java are?

Iván: Yes, we have an issue pending for that and it’s included in our future roadmap. Please see https://github.com/micronaut-projects/micronaut-core/issues/675

How does Micronaut behave with Lombok?

Iván: It works without any problem. The only thing you need to take care of is adding Lombok annotationProcessor dependencies before Micronaut dependencies.

Does Micronaut supports Vaadin?

Iván: We don’t have a specific integration with Vaadin, but I think you can use the library directly. Micronaut can serve static resources, take a look at https://docs.micronaut.io/latest/guide/index.html#staticResources

Can I find the example project on GitHub?

Iván: I can’t share the code of the project I showed here, but in this repository https://github.com/ilopmar/micronaut-testing-best-practices/ you can find a similar structure to the demo project.

Can swagger generate a schema based on the controller class definition?

Iván: If you mean a .class, then the answer is no. The swagger/openapi YAML file is generated at compile time, so Micronaut needs to compile the code to be able to generate the swagger/openapi file.

How different is it from Spring Cloud Alibaba?

Iván: From what I see in their repository, they already provide a set of different tools configured for a specific purpose. With Micronaut and the integrations we have with third-party libraries, you can achieve the same.

Do you have support for Metric Registry (or) health metrics?

Iván: Yes. We support Micrometer with a lot of different backends, see https://micronaut-projects.github.io/micronaut-micrometer/latest/guide/index.html. We also provide other management and health endpoints: https://docs.micronaut.io/latest/guide/index.html#management

Any advice for using Micronaut and Kotlin? I had issues using the JetBrains compiler with a Kotlin-based Micronaut application even with the enabled annotation processing. Just delegate to Gradle or other ideas?

Iván: The integration from the command line using Gradle should work without any problems. Regarding the integration with IntelliJ IDEA, there is an open issue in the Kotlin issue tracker to improve it: https://youtrack.jetbrains.com/issue/KT-15040.

Which frontend framework integrates best with Micronaut?

Iván: Any frontend framework you want and feel comfortable with. If you create a RESTful JSON API with Micronaut, you can use any frontend: Vue.JS, React, Angular, jQuery, and so on.

Our next webinar

For all our IntelliJ IDEA users, we are hosting another webinar on ‘Working with SQL and Databases in IntelliJ IDEA‘.

Join us on Friday, October 16, 4:00 pm – 5:00 pm CEST. Register now.

Follow us on Twitter so that you don’t miss the updates.

Happy developing!

image description