Spring Boot Hello World Using IntelliJ IDEA
Author: Deep Singh | Category: Java, Spring Boot, IntelliJ IDEA
Introduction
In this tutorial, you'll learn how to create your very first Spring Boot Hello World application using IntelliJ IDEA and Spring Initializr. This beginner-friendly guide walks you through every step — from downloading the IDE to testing the running REST endpoint in your browser.
By the end, you'll have a working Spring Boot application that responds with "Hello World Example" when accessed via a web browser.
Step 1: Download and Install IntelliJ IDEA
First, download and install IntelliJ IDEA on your machine:
Step 2: Open Spring Initializr
After installing IntelliJ IDEA, open Spring Initializr to generate your Maven project:
Spring Initializr Download Here
Step 3: Configure Project and Add Dependencies
Change the Group ID, Artifact ID, and Package Name according to your project. Then, from the top-right corner, add the Spring Web dependency and click Generate:
Step 4: Open the Project in IntelliJ IDEA
After downloading your project, extract it. Then in IntelliJ IDEA, select File → Open:
Then select the extracted project folder:
Step 5: Project Structure
Once the project finishes loading, your project structure will look like this:
Step 6: Create HelloController.java
Create a new class inside the same package as HelloworldApplication and name it HelloController.java:
package com.deepsingh44.helloworld;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/")
public String welcome() {
return "Hello World Example";
}
}
Step 7: Run the Application
Right-click on HelloworldApplication and select Run:
Step 8: Change Server Port (Optional)
If the default server port is already in use, you'll get a Port already in use error. To fix this, open application.properties and change the port:
server.port=8883
Step 9: Test in Browser
After the server starts successfully, open any browser and enter the URL (http://localhost:8080/ or your custom port) and hit Enter to see the result:
Conclusion
Congratulations! You've successfully created and run your first Spring Boot Hello World application using IntelliJ IDEA. This is the foundation for building more complex Spring Boot applications.
Key takeaways:
- Spring Initializr is the fastest way to bootstrap a Spring Boot project with the right dependencies.
- @RestController combines
@Controllerand@ResponseBody, making it ideal for building REST endpoints. - @GetMapping("/") maps HTTP GET requests to the
welcome()method. - Spring Boot's embedded Tomcat runs the app on port 8080 by default (configurable via
server.port). - IntelliJ IDEA provides excellent tooling for Spring Boot, including run configurations and dependency management.
Next steps to explore:
- Add more REST endpoints with
@PostMapping,@PutMapping, and@DeleteMapping - Integrate a database with Spring Data JPA
- Add Spring Security for authentication
- Build a complete CRUD REST API
- Package your app as a JAR and deploy it
This simple Hello World project is your gateway to the entire Spring Boot ecosystem. Happy coding!
No comments:
Post a Comment