Spring Boot Hello World Example

Spring Boot Hello World Example

This spring boot tutorial shows how to create a basic Hello World application using IntelliJ IDEA. Nowadays Spring Boot is a very popular technique for creating microservices. It is an open-source Java-based framework. Comparing to Spring it gives more benefits to the developer such as annotation configurations and default codes, more flexible, configuration is easy, no manual XML configurations like Spring because it creates automatically and it saves the development time.

Spring boot provides Database transaction interfaces so easy to communicate with MySQL, Cassandra, or MongoDB, and it more compatible to develop rest endpoints and it provides POM file it helps to manage dependencies in your project furthermore Spring Boot is providing embedded servlet container so you can use web servers like Tomcat to deploy your project.

Below steps help you to build the Spring Boot Hello world application. You can create a fresh Spring Boot application from here.

Firstly we have to understand the structure of the project since it is a more important point in Spring Boot as well as Spring, This is the structure of a project.

Spring Boot | Hello World | Spring | Micro service |

Secondly, better understand the pom file. This will create automatically, all your dependencies should define here. E.g – MySQL connector, Embedded Tomcat, JUnit.
Below is the pom file.

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.7.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>learnspringboot</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>learnspringboot</name>
	<description>test app</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

Then create the Controller class I named it as “FirstController.java” and past or write this code in to it.

package com.example.learnspringboot.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FirstController {

    //URL -

    @RequestMapping("/")
    public String sayHello() {
        return "Hello";
    }

    @RequestMapping("/hello/world")
    public String helloWorld() {
        return "Hello World !!!!!";
    }

}

Finally, you can run the project and open the web browser and hit this on the URL bar. http://localhost:8080/hello/world then it will return “Hello World !!!!!”

After learning this tutorial you can understand the GET and POST methods in Spring Boot referring to my other articles.

Git Location – https://github.com/rajithabhanuka/Learnspringboot.git