Monday, 2 January 2023

Micronaut hello world application in Eclipse

In this post, I am going to explain a hello world project in Eclipse.

 

Step 1: Create new maven project ‘hello-world-eclipse’.

 

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>hello-world-eclipse</artifactId>
	<version>1</version>

	<parent>
		<groupId>io.micronaut</groupId>
		<artifactId>micronaut-parent</artifactId>
		<version>3.7.0</version>
	</parent>

	<properties>
		<packaging>jar</packaging>
		<jdk.version>11</jdk.version>
		<release.version>11</release.version>
		<micronaut.version>3.7.0</micronaut.version>
		<micronaut.runtime>netty</micronaut.runtime>
		<exec.mainClass>com.sample.app.App</exec.mainClass>
	</properties>

	<repositories>
		<repository>
			<id>central</id>
			<url>https://repo.maven.apache.org/maven2</url>
		</repository>
	</repositories>

	<dependencies>
		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-inject</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-validation</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-http-client</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-http-server-netty</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-jackson-databind</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>jakarta.annotation</groupId>
			<artifactId>jakarta.annotation-api</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>io.micronaut.test</groupId>
			<artifactId>micronaut-test-junit5</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-api</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-engine</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>io.micronaut.build</groupId>
				<artifactId>micronaut-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

 

<exec.mainClass> specifies main class to run the application.

 

Step 3: Define HelloWorldController class.

 

HelloWorldController.java

package com.sample.app.controller;

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

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

	@Get(produces = MediaType.TEXT_PLAIN)
	public String index() {
		return "Hello World";
	}
}

 

a.   The @Controller annotation defines the class as a controller mapped to the path /hello

b.   The @Get annotation maps the index method to all requests that use an HTTP GET

c.    A String "Hello World" is returned as the response

 

 

Step 4: Define main application class.

 

App.java

package com.sample.app;

import io.micronaut.runtime.Micronaut;

public class App {
	public static void main(String[] args) {
		Micronaut.run(App.class, args);
	}
}

 

Total project structure looks like below.

 


How to get the jar file?

Navigate to the folder where pom.xml is located. Execute the command ‘mvn clean install’.

 

Upon command execution, you can see a jar file ‘hello-world-eclipse-1.jar’ is created in target folder.

 

How to run the jar file?

Run the jar file by executing below command.

java -jar ./target/hello-world-eclipse-1.jar

 

Once the application started successfully, open the url ‘http://localhost:8080/hello’ in browser, you will see following kind of screen.

 


 

You can download this application from this link.

 

In my next post, I am going to explain how to test hello world app using Micronaut Http Client.


 

Previous                                                    Next                                                    Home

PlantUML : Create a sudoku

‘sudoku’ command draws a sudoku.

 

sudoku.txt

@startuml
sudoku
@enduml

 

Above snippet generate below diagram.

 


We can pass custom seed to the sudoku command.

 

sudokuCustomSeed.txt

@startuml
sudoku abcd12789eo0
@enduml

 

Above snippet generate below diagram.


 

 

Previous                                                    Next                                                    Home

Micronaut: Hello world application using Micronaut CLI

Prerequiste

Setup Micronaut CLI

 

Step 1: Open terminal and execute below command to create a Maven-based project.

mn create-app hello-world --build maven

 

Generated project structure looks like below.

bash-3.2$ tree
.
|____hello-world
| |____pom.xml
| |____README.md
| |____.gitignore
| |____.mvn
| | |____wrapper
| | | |____MavenWrapperDownloader.java
| | | |____maven-wrapper.properties
| | | |____maven-wrapper.jar
| |____mvnw
| |____micronaut-cli.yml
| |____mvnw.bat
| |____src
| | |____test
| | | |____java
| | | | |____hello
| | | | | |____world
| | | | | | |____HelloWorldTest.java
| | |____main
| | | |____resources
| | | | |____logback.xml
| | | | |____application.yml
| | | |____java
| | | | |____hello
| | | | | |____world
| | | | | | |____Application.java

 

Step 2: Navigate to hello-world folder (where pom.xml is located) and execute below command to run the application.

./mvnw mn:run

$ ./mvnw mn:run
[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------------< hello.world:hello-world >-----------------------
[INFO] Building hello-world 0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] >>> micronaut-maven-plugin:3.4.0:run (default-cli) > process-classes @ hello-world >>>
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ hello-world ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ hello-world ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/krishna/Documents/technical-documents/micronaut/examples/hello-world/target/classes
[INFO] 
[INFO] <<< micronaut-maven-plugin:3.4.0:run (default-cli) < process-classes @ hello-world <<<
[INFO] 
[INFO] 
[INFO] --- micronaut-maven-plugin:3.4.0:run (default-cli) @ hello-world ---
 __  __ _                                  _   
|  \/  (_) ___ _ __ ___  _ __   __ _ _   _| |_ 
| |\/| | |/ __| '__/ _ \| '_ \ / _` | | | | __|
| |  | | | (__| | | (_) | | | | (_| | |_| | |_ 
|_|  |_|_|\___|_|  \___/|_| |_|\__,_|\__,_|\__|
  Micronaut (v3.7.0)

16:28:50.547 [main] INFO  io.micronaut.runtime.Micronaut - Startup completed in 633ms. Server Running: http://localhost:8080

 

Upon successful command execution, open the url in ‘http://localhost:8080’ in browser.

 



You can download this application from this link. In my next post, I will explain how to create a Rest API.

 

Previous                                                    Next                                                    Home

PlantUML: Hello world application

loginSuccessFlow.txt

@startuml
Client -> LoginServer: Enter user name
Client -> LoginServer: Enter password
Client -> LoginServer: Submit the form 
LoginServer -> ADServer: Check the credentials
ADServer -> LoginServer: Login Successful
LoginServer -> Client: Login Successful
@enduml

 

Execute below command to generate the sequence diagram.

java -jar plantuml.jar loginSuccessFlow.txt

 You can download plantuml.jar file from this location.

Upon successful command execution, you can see 'loginSuccessFlow.png' file.

 

loginSuccessFlow.png


 

 

Previous                                                    Next                                                    Home

PlantUML: setup the environment locally

Install graphviz

 

On Mac

brew install graphviz (or)

sudo port install graphviz

 

Ubuntu

sudo apt update

sudo apt install graphviz

Previous                                                    Next                                                    Home

Saturday, 31 December 2022

Introduction to PlantUML

PlantUML is a java library, where it take the input in PlantUML language and generate the diagram.

 

For example, following markup defines the successful login flow sequence diagram.


@startuml
Client -> LoginServer: Enter user name
Client -> LoginServer: Enter password
Client -> LoginServer: Submit the form 
LoginServer -> ADServer: Check the credentials
ADServer -> LoginServer: Login Successful
LoginServer -> Client: Login Successful
@enduml

Above markup generate below sequence diagram.

 


PlantUML is a java library that allows you to quickly write:

a.   Sequence diagram

b.   Usecase diagram

c.    Class diagram

d.   Object diagram

e.   Activity diagram (here is the legacy syntax)

f.     Component diagram

g.   Deployment diagram

h.   State diagram

i.     Timing diagram

 

Is there any support for non-uml diagrams?

The following non-UML diagrams are also supported:

a.   JSON data

b.   YAML data

c.    Extended Backus-Naur Form (EBNF) diagram

d.   Network diagram (nwdiag)

e.   Wireframe graphical interface or UI mockups (salt)

f.     Archimate diagram

g.   Specification and Description Language (SDL)

h.   Ditaa diagram

i.     Gantt diagram

j.     MindMap diagram

k.    Work Breakdown Structure diagram (WBS)

l.     Mathematic with AsciiMath or JLaTeXMath notation

m. Entity Relationship diagram (IE/ER)

 

 

References

https://plantuml.com/

Previous                                                    Next                                                    Home