Effective Java with Groovy - Favour Internal Iterators

Let's start with the following code making use of the traditional for loop. List<String> languages = List.of("Java", "Groovy", "Kotlin"); // List.of() requires Java 9 or above for (int i = 0; i < languages.size(); i++) { System.out.println(languages.get(i)); } Compare the above code to…

Groovy Scripts - Functions and Variables

In a previous post, we explored the simplicity of scripting mode offered by Groovy. We arrived at the following code. String message = args.length > 0 ? "Hello ${args[0]}" : "Hello" println message Let's make the functionality slightly more sophisticated. Say, we want to send a custom message as the optional…

Micronaut HTTP Client - just the response body

In a previous post, we used io.micronaut.http.client.BlockingHttpClient to get response from an HTTP endpoint. We used the exchange method as follows. HttpResponse response = client.toBlocking().exchange("/greeting", String) If you are using Java instead of Groovy, you will pass "String.class" instead of "String".We had…

Groovy Scripts - How do they work?

If you are already familiar with Groovy, you know that Groovy supports script mode where you can write Groovy code without explicitly creating a class. This mode is suitable when you want to house the entire code in a single class. If you are new to Groovy, you might want…

SDKMAN - A must-have for all developers using JVM

Did you ever have to work with multiple Java projects and each of them was on different version Java or any JVM tools? SDKMAN is a CLI tool for managing multiple versions of Java and JVM tools. I started using this tool in its earlier form known as GVM(Groovy…

Micronaut HTTP Client - Parsing Response

I am using Micronaut 2.0 in this post. You can verify your version using the mn -Version command. Let us generate a controller using the create-controller command provided by the Micronaut CLI as follows. I am launching the CLI from myapp directory. ➜ myapp mn mn> create-controller com.nareshak.…

A Feel of Groovy DSL with an Example from Gradle

If you have used Gradle build system, probably you have already been used and benefited from the DSL capabilities of Groovy language. In this post, let us take a peek at such an example. Consider the following piece of DSL from a Gradle configuration. plugins { id 'java' } The plugins section…