YouTube Video
We recommend watching the YouTube video below to learn how to create a Spring Boot project in Eclipse IDE using Spring Initializr and build Spring Boot REST API:
What You Need Before Starting
Java Development Kit (JDK 17 or higher):
Spring Boot 3 requires Java 17 or above.Eclipse IDE:
Download and install the latest version of Eclipse IDE for Enterprise Java Developers from https://www.eclipse.org/downloads/.
Step 1: Create a Spring Boot Project Using Spring Initializr
Spring Initializr is a web-based tool that helps generate Spring Boot projects with the necessary configurations and dependencies.
- Open your browser and go to Spring Initializr.
- Fill in the project details:
- Group: Enter your organization or package name, e.g.,
com.example
. - Artifact: Enter your project name, e.g.,
spring-boot-rest-api
. - Language: Select Java.
- Packaging: Select Jar (default).
- Java Version: Choose 17 or later.
- Group: Enter your organization or package name, e.g.,
- Add dependencies:
- Click Add Dependencies and select:
- Spring Web: To build REST APIs.
- Spring Boot DevTools: For hot-reloading during development.
- Spring Data JPA: To interact with databases.
- H2 Database: For an in-memory database (optional).
- Click Add Dependencies and select:
- Click Generate to download the project as a ZIP file.
Step 2: Import the Spring Boot Project into Eclipse
Open Eclipse IDE:
- Launch Eclipse and choose a workspace where your projects will be stored.
Import the Project:
- Go to File > Import.
- Select Existing Maven Projects (since the Spring Boot project uses Maven).
- Click Next.
Locate the Project Folder:
- Browse to the folder where you extracted the ZIP file from Spring Initializr.
- Click Finish to import the project.
Wait for Eclipse to Download Dependencies:
- Eclipse will automatically download the necessary dependencies mentioned in the
pom.xml
file.
- Eclipse will automatically download the necessary dependencies mentioned in the
Step 3: Explore the Project Structure
Once the project is imported, you’ll see a structure similar to this:
spring-boot-rest-api/
├── src/
│ ├── main/
│ │ ├── java/com/example/springbootrestapi/
│ │ │ └── SpringBootRestApiApplication.java
│ │ └── resources/
│ │ ├── application.properties
│ ├── test/
│ └── java/
├── pom.xml
- SpringBootRestApiApplication.java: The main entry point of your Spring Boot application.
- application.properties: Configuration file for your application.
- pom.xml: Maven file for managing dependencies.
Step 4: Build a Simple REST API
Now, let’s add a REST controller to handle HTTP requests.
Create a New Java Class:
- Right-click on the package
com.example.springbootrestapi
in thesrc/main/java
folder. - Select New > Class.
- Name it
HelloController
and click Finish.
- Right-click on the package
Add REST API Code:
- Open the
HelloController.java
file and add the following code:package com.example.springbootrestapi; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/") public String sayHello() { return "Welcome to Spring Boot!"; } }
- Open the
Step 5: Run the Spring Boot Application
Run the Application:
- Open the
SpringBootRestApiApplication.java
file in thesrc/main/java/com/example/springbootrestapi
folder. - Right-click on the file and select Run As > Java Application.
- Open the
Verify the Application:
- Once the application starts, the console will display a message like:
Tomcat started on port(s): 8080 (http)
- Open your browser and go to
http://localhost:8080/
. - You should see the response:
Welcome to Spring Boot!
- Once the application starts, the console will display a message like:
Step 6: Customize Application Properties (Optional)
You can modify the application.properties
file in the src/main/resources
folder to customize your application settings. For example:
- Change the server port:
server.port=8081
Step 7: Test Your REST API
For more advanced testing:
- Postman: Use Postman to send HTTP GET, POST, PUT, or DELETE requests to your API.
- Browser: You can use your browser to make simple GET requests.
Conclusion
You’ve successfully created a Spring Boot project using Spring Initializr, imported it into Eclipse IDE, and built a simple REST API. With these tools, you can start developing powerful Java applications. Keep experimenting with more features of Spring Boot, like connecting to a database or creating complex endpoints. Happy coding!
Comments
Post a Comment
Leave Comment