Open In App

Spring Boot – Hello World

Improve
Improve
Like Article
Like
Save
Share
Report

Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and setup. Spring Boot is a microservice-based framework and making a production-ready application in it takes very little time. In this article, we will discuss Hello World Example using the spring boot. We will discuss two ways to print Hello World using Spring Boot.

  • With the help of the CommandRunner interface of SpringBoot
  • With the controller class in the SpringBoot

First, initialize the project on our machine. Spring Initializr is a web-based tool using which we can easily generate the structure of the Spring Boot project. It also provides various different features for the projects expressed in a metadata model. This model allows us to configure the list of dependencies that are supported by JVM. Here, we will create the structure of an application using a spring initializer and then use an IDE to create a sample GET route. Therefore, to do this, the following steps are followed sequentially as follows.

Step-by-Step Implementation

Step 1: Go to Spring Initializr

Fill in the details as per the requirements. For this application:

Project: Maven
Language: Java
Spring Boot: 2.2.8
Packaging: JAR
Java: 8
Dependencies: Spring Web
STS IDE

Step 2: Provide the Group name in the “Group” field. Here we have named it as “com.example”
Step 3: Provide the Artifact ID in the “Artifact” field, which is the unique name of the project. Here we have provided it’s name as “Spring boot app”
Step 4: Add dependencies which are needed based on the project
Step 5: Click on Generate which will download the starter project

Spring Initializr

Step 6: Extract the zip file. Now open STS IDE and then go to File > Import> Existing Maven Project > Next > Browse > Select the Project Folder > Finish.

After the project imports successfully, it will look like pictorially depicted below as follows:

Package Explorer

Note: In the Import Project for Maven window, make sure you choose the same version of JDK which you selected while creating the project.

Method 1: With the help of the CommandRunner interface of SpringBoot

Step 7: Go to src > main > java > com.geeksforgeeks, Below is the code for the SpringBootHelloWorldApplication.java file.

Java




@SpringBootApplication
// Main class
// Implementing CommandLineRunner interface
public class SpringBootHelloWorldApplication
    implements CommandLineRunner {
    // Method 1
    // run() method for springBootApplication to execute
    @Override
    public void run(String args[]) throws Exception
    {
        // Print statement when method is called
        System.out.println("HEllo world");
    }
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Calling run() method to execute
        // SpringBootApplication by
        // invoking run() inside main() method
        SpringApplication.run(
            SpringBootHelloWorldApplication.class, args);
    }
}


This application is now ready to run.

Step 8: Run the SpringBootHelloWorldApplication class and wait for the Tomcat server to start where the default port is already set.

Tip: The default port of the Tomcat server is 8080 and can be changed in the application.properties file.

Output: Generated on terminal/CMD 

Method 2: With the controller class in the SpringBoot

Step 9: Go to src > main > java > com.geeksforgeeks and create a controller class. Below is the code for the controller.java file.

Java




package com.geeksforgeeks.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
  
@RestController
public class HelloWorldController {
    @RequestMapping("/") public String helloworld()
    {
        return "Hello World";
    }
}


This controller helps to handle all the incoming requests from the client-side.

When the application runs successfully, it will show the the message that the application has started in the default port number 8080 as shown in following image:

output-image

Step 10: Now we will use the PostMan and call the get API of the Spring boot application.

POSTMAN Screen



Last Updated : 10 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads