How to create hello world app using spring boot framework

1. Introduction

Spring Boot is a popular framework used for building Java applications. It simplifies the process of setting up and running applications by providing a range of features out of the box. Using Spring Boot with Eclipse IDE makes the development process even smoother due to Eclipse’s powerful features and integrations.

In this tutorial, we’ll walk you through creating a simple “Hello World” application using Spring Boot in Eclipse IDE. This is a great way to get started with Spring Boot and understand the basics of how it works.

2. Prerequisites

Before we begin, ensure you have the following installed:

  • Java Development Kit (JDK): Download and install the latest JDK from Oracle’s official site.
  • Eclipse IDE: Download and install the Eclipse IDE for Java Developers from Eclipse’s official site.
  • Spring Tool Suite (STS) Plugin: Although optional, installing the STS plugin for Eclipse simplifies the process of creating and managing Spring Boot projects. It can be installed via Eclipse Marketplace.

Ensure that your environment variables are correctly set up, particularly the JAVA_HOME and PATH variables pointing to your JDK installation.

3. Creating a New Spring Boot Project in Eclipse

  1. Open Eclipse IDE and navigate to File > New > Other.
  2. In the dialog box, expand Spring Boot and select Spring Starter Project. Click Next.
  3. Project Metadata:
  • Name: hello-world-spring-boot
  • Type: Maven Project
  • Packaging: Jar
  • Java Version: 11 (or your installed version) Click Next.
  1. Dependencies: In the dependencies section, select Spring Web and click Finish. Eclipse will create the project structure and download the necessary dependencies.

4. Setting Up the Main Application

Once the project is created, you’ll see the default structure. Let’s set up the main application class.

  1. Navigate to src/main/java and locate the main package. By default, it will be named based on your group ID and artifact ID.
  2. Create the Main Application Class: Right-click the package, select New > Class, and name it HelloWorldApplication.
package com.example.helloworld;

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

@SpringBootApplication
public class HelloWorldApplication {

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

5. Adding the Controller

A controller in Spring Boot handles HTTP requests and returns responses.

  1. Create a New Package: Right-click the main package, select New > Package, and name it controller.
  2. Create a Controller Class: Right-click the controller package, select New > Class, and name it HelloWorldController.
package com.example.helloworld.controller;

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

@RestController
public class HelloWorldController {

    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, World!";
    }
}

This class uses the @RestController annotation to define a RESTful web controller and the @GetMapping annotation to map HTTP GET requests to the /hello endpoint.

6. Running the Application

  1. Run the Application: Right-click the HelloWorldApplication class and select Run As > Spring Boot App.
  2. Test the Application: Open a web browser and navigate to http://localhost:8080/hello. You should see the message “Hello, World!”.

7. Conclusion

Congratulations! You’ve successfully created a simple “Hello World” application using Spring Boot in Eclipse IDE. This tutorial covered the basics of setting up a Spring Boot project, adding a controller, and running the application.

For further learning, consider exploring more about Spring Boot features such as dependency injection, Spring Data JPA, and Spring Security. The Spring Boot documentation is an excellent resource for diving deeper into these topics.

Related Posts

Samsung New S24 Release Update.

The Samsung Galaxy S24, S24 Plus, and S24 Ultra were just released in January 2024, so they’re packed with the latest and greatest tech. Here’s a quick…

How to get DUNS Number?

Sure, here’s an article on how to obtain a DUNS number: To obtain a DUNS number, you can follow the below steps: To obtain a DUNS number…

gopro

How to Fix GoPro SD card Error

Troubleshooting and Fixing GoPro SD Card Errors Introduction:GoPro cameras are popular for capturing thrilling adventures, but occasionally users may encounter SD card errors that can disrupt the…

Gemini

A Revolution in Artificial Intelligence 

Unveiling Google Gemini.ai   Google has set the stage for a groundbreaking advancement in the field of artificial intelligence with the introduction of Gemini.ai, an ambitious project…

Leave a Reply

Your email address will not be published. Required fields are marked *