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
- Open Eclipse IDE and navigate to
File > New > Other
. - In the dialog box, expand
Spring Boot
and selectSpring Starter Project
. ClickNext
. - Project Metadata:
- Name:
hello-world-spring-boot
- Type: Maven Project
- Packaging: Jar
- Java Version: 11 (or your installed version) Click
Next
.
- Dependencies: In the dependencies section, select
Spring Web
and clickFinish
. 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.
- Navigate to
src/main/java
and locate the main package. By default, it will be named based on your group ID and artifact ID. - Create the Main Application Class: Right-click the package, select
New > Class
, and name itHelloWorldApplication
.
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.
- Create a New Package: Right-click the main package, select
New > Package
, and name itcontroller
. - Create a Controller Class: Right-click the
controller
package, selectNew > Class
, and name itHelloWorldController
.
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
- Run the Application: Right-click the
HelloWorldApplication
class and selectRun As > Spring Boot App
. - 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.