🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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.xmlfile.
- 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.springbootrestapiin thesrc/main/javafolder. - Select New > Class.
- Name it
HelloControllerand click Finish.
- Right-click on the package
Add REST API Code:
- Open the
HelloController.javafile 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.javafile in thesrc/main/java/com/example/springbootrestapifolder. - 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!
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment