Monday 17 April 2023

Micronaut normalizes the properties into kebab case

Micronaut normalizes the properties in kebab case internally.

 

What is kebab case?

In kebab case, spaces in a string are replaced with hyphen.

 

For example,

‘my app name’ is converted to my-app-name in kebab case.

 

Even though Micronaut support camel case properties, it internally normalize the property into a kebab case.

 

For example,

chatServerVersion is interpreted as chat-server-version

chatServer.contactDetails is interpreted as chat-server.contact-details

 

Following two statements return the value that is associated with the property ‘chatServerVersion’.

String chatServerVersion1 = environment.getProperty("chatServerVersion", String.class, "No chat server version found");
String chatServerVersion2 = environment.getProperty("chat-server-version", String.class, "No chat server version found");

 

Find the below working application.

 

Step 1: Create new maven project ‘micronaut-kebab-case’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

 

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>micronaut-kebab-case</artifactId>
	<version>1</version>

	<properties>
		<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
		<micronaut.version>3.7.3</micronaut.version>
		<slf4j.version>2.0.3</slf4j.version>
		<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>

		<maven.compiler.target>15</maven.compiler.target>
		<maven.compiler.source>15</maven.compiler.source>
	</properties>

	<dependencies>

		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-inject-java</artifactId>
			<version>${micronaut.version}</version>
		</dependency>
		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-runtime</artifactId>
			<version>${micronaut.version}</version>
		</dependency>

		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-validation</artifactId>
			<version>${micronaut.version}</version>
		</dependency>


		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-simple</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>${maven-compiler-plugin.version}</version>
				<configuration>
					<annotationProcessorPaths>
						<path>
							<groupId>io.micronaut</groupId>
							<artifactId>micronaut-validation</artifactId>
							<version>${micronaut.version}</version>
						</path>
					</annotationProcessorPaths>
				</configuration>
			</plugin>

			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<archive>
						<manifest>
							<mainClass>com.sample.app.App</mainClass>
						</manifest>
					</archive>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
				</configuration>

				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

		</plugins>
	</build>
</project>

 

Step 3: Create application.yml file in src/main/resources folder.

 

application.yml
chatServerVersion: 1.23.4
chatServer:
  contactDetails: demo-chat-server@domain.com

 

Step 4: Define main application class.

 

App.java

 

package com.sample.app;

import io.micronaut.context.ApplicationContext;
import io.micronaut.context.env.Environment;

public class App {

	public static void main(String[] args) {

		try (ApplicationContext applicationContext = ApplicationContext.run()) {

			Environment environment = applicationContext.getEnvironment();

			String chatServerVersion1 = environment.getProperty("chatServerVersion", String.class,
					"No chat server version found");
			String chatServerVersion2 = environment.getProperty("chat-server-version", String.class,
					"No chat server version found");
			
			String chatServerContactDetails1 = environment.getProperty("chatServer.contactDetails", String.class,
					"No contact details found");
			String chatServerContactDetails2 = environment.getProperty("chat-server.contact-details", String.class,
					"No contact details found");
			
			System.out.println("chatServerVersion1 : " + chatServerVersion1);
			System.out.println("chatServerContactDetails1 : " + chatServerContactDetails1);
			System.out.println("chatServerVersion2 : " + chatServerVersion2);
			System.out.println("chatServerContactDetails2 : " + chatServerContactDetails2);
		}

	}
}

 


 

Build the project using mvn package command.

Navigate to the folder where pom.xml is located and execute the command ‘mvn package’.

 

Upon command successful execution, you can see the jar file ‘micronaut-kebab-case-1-jar-with-dependencies.jar’ in project target folder.

$ ls ./target/
archive-tmp
classes
generated-sources
generated-test-sources
maven-archiver
maven-status
micronaut-kebab-case-1-jar-with-dependencies.jar
micronaut-kebab-case-1.jar
test-classes

Execute below command to run the application.

$ java -jar ./target/micronaut-kebab-case-1-jar-with-dependencies.jar
chatServerVersion1 : 1.23.4
chatServerContactDetails1 : demo-chat-server@domain.com
chatServerVersion2 : 1.23.4
chatServerContactDetails2 : demo-chat-server@domain.com

You can download this application from this link.


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment