Springboot Hello World

Springboot Hello World Example

This is a simple Springboot application that shows how to create a simple Hello World application.

Boostraping project

  1. Create a new project using Spring Initializr .

  2. Choose gradle or maven project.

  3. Choose spring web dependency.

    spring boot initializr

  4. Explore the project and generate zip.

  5. Unzip the file and open the project in your favourite IDE.

Creating the controller

  1. Create a controller class in com.example.springboot.controller package.

  2. Naming convention for controller class is {controller-name}Controller.

  3. Name the controller class as HelloController.java as we are going to create hello world app.

  4. Add @RestController annotation to the controller class to specify that this is a rest controller and is also responsible to serialize java objects to json.

  5. Add the @RequestMapping annotation to the controller method to specify the url path.

  6. Add the following code to the HelloController.java class.

    1@RestController
    2@RequestMapping("/hello")
    3public class HelloController {
    4    @RequestMapping("/")
    5    public String hello() {
    6        return "Hello World";
    7    }
    8}
    

Running the application

  1. Run the application using gradle, maven or from main class.
  2. Using Gradle: gradle bootRun .
  3. Using Maven: mvn spring-boot:run.
  4. Using main class: Run the application using main() method.
  5. Now you can access the application at http://localhost:8080/hello.
  6. You can see the hello world message.