Hello World Example in Spring Boot

In this article, we will create a simple example in Spring Boot which will be nothing but a Hello World Example.

We will be creating an application using the Spring Tool Suite. Spring Tool Suite provides the IDE(Integrated Development Environment) for developing world-class Enterprise applications. If you want to know How to configure STS on your machine you can check here How to Create a Spring Boot project using Spring Initializr?

Spring Boot Hello World Example

Step 1: Open IDE STS- Spring Tool Suite

Step 2: Go to File > Spring Starter Project.

Step 3: Now, Fill all the fields as shown below and click Next.

Step 4: Now, Add the dependencies as per your requirement, I have added Spring Web Dependency and click Next > Finish.

Step 5: Now, It will take time to load the projects and the dependencies and your Project Structure will look like this. 

Step 6: Create a Controller Class HomeController inside the com.abc package.

Step 7: Create a method home() inside the HomeController class that will return the String.

package com.abc;


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

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

@RestController
public class HomeController {

  @RequestMapping(value = "/")
  public String home() {
    return "Hello World ! Welcome to Codedec";
  }
}
  • @RestController annotation is used to combine both @Controller & @ResponseBody where @Controller is nothing but is used to auto-detect implementation classes through the classpath scanning.
  • Talking about @ResponseBody tells a controller that the object returned to the HTTP response body.
  • In this way, @RestController is used to combine both the annotation.

Step 8: Run the SpringBootHelloWorldProgramApplication file.

package com.abc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootHelloWorldProgramApplication {

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

}

Step 9: Now, in case if you get the following error.

Step 10: If you get the above error, Go to the application. properties and change the port number to anything. ( I have changed to 8888). see the following code

#Change the port number
server.port=8888

Step 11: If everything is good, then just stop the server and run SpringBootHelloWorldProgramApplication once again. It will show the message shown in the red box.

Step 12: Go to any browser and type localhost:8888, It will show the text we have written in the home() method of the controller class.

In this way, we create a simple Hello World Example in Spring Boot.