Spring Boot is a powerful framework for building Java-based backend applications. It simplifies the setup process and allows developers to create stand-alone, production-ready applications with minimal configuration.
In this guide, we will walk through the Spring Boot setup process, covering everything from installation to running your first Spring Boot application. By the end, you'll have a fully functional Java backend project.
Prerequisites
Before setting up a Spring Boot project, ensure you have the following installed:
- Java Development Kit (JDK 17 or later)
- Maven or Gradle (for dependency management)
- IDE (IntelliJ IDEA, Eclipse, or VS Code)
- Spring Boot CLI (optional)
Checking Java Installation
To verify if Java is installed, open a terminal or command prompt and run:
java -versionIf Java is not installed, download and install it from the Oracle JDK or OpenJDK.
Step 1: Creating a Spring Boot Project
There are multiple ways to create a Spring Boot project:
1.1 Using Spring Initializr (Recommended)
Spring Initializr is an online tool for generating Spring Boot projects.
- Go to Spring Initializr
- Select the following options:
- Project: Maven / Gradle
- Language: Java
- Spring Boot Version: Latest Stable Version
- Dependencies: Spring Web, Spring Boot DevTools, Lombok (optional), and H2 Database (optional)
- Click Generate to download the project as a ZIP file.
- Extract the ZIP and open it in your IDE.
1.2 Using Spring Boot CLI
If you have Spring Boot CLI installed, run the following command:
spring init --name=my-spring-boot-app --dependencies=web my-spring-boot-appThis will generate a new Spring Boot project with Spring Web dependency.
Step 2: Understanding the Project Structure
Once you open the project in an IDE, you will see the following structure:
my-spring-boot-app/
├── src/
│ ├── main/
│ │ ├── java/com/example/demo/
│ │ │ ├── DemoApplication.java (Main Class)
│ │ ├── resources/
│ │ │ ├── application.properties (Configuration File)
│ ├── test/ (Test cases)
├── pom.xml (Maven Build File)Key Files:
DemoApplication.java: The main entry point of the Spring Boot application.application.properties: Configuration file for the application.pom.xmlorbuild.gradle: Manages project dependencies.
Step 3: Running the Spring Boot Application
To run the project, navigate to the root folder and use one of the following methods:
Using Maven:
mvn spring-boot:runUsing Gradle:
gradle bootRunRunning as a Java Application
You can also run the project directly from the main class:
java -jar target/my-spring-boot-app-0.0.1-SNAPSHOT.jarOnce started, the application will be available at:
http://localhost:8080Step 4: Creating a Simple REST API
Now, let's create a simple REST Controller in Spring Boot.
Creating a Controller Class
Inside the com.example.demo package, create a new class HelloController.java:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, Spring Boot!";
}
}Testing the API
Run the Spring Boot application and open a browser or use Postman to access:
http://localhost:8080/api/helloExpected Output:
Hello, Spring Boot!Step 5: Configuring Application Properties
Spring Boot uses the application.properties or application.yml file for configuration.
Example application.properties:
server.port=9090
spring.application.name=MySpringBootAppRestart the application, and now it will run on port 9090 instead of the default 8080.
Step 6: Connecting to a Database
Spring Boot supports multiple databases, including MySQL, PostgreSQL, and H2. Let's configure an H2 in-memory database for this project.
Adding Database Dependencies (For Maven)
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>Updating application.properties:
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=trueNow, access the H2 database console at:
http://localhost:9090/h2-consoleUse JDBC URL: jdbc:h2:mem:testdb
Common Errors & Troubleshooting
1. Port Already in Use
Error:
Port 8080 is already in useSolution:
Change the port in application.properties:
server.port=90902. ClassNotFoundException: org.h2.Driver
Solution:
Ensure that the H2 dependency is included in pom.xml.
Spring Boot Interview Questions
- What is Spring Boot and how is it different from Spring Framework?
- How does Spring Boot handle dependency management?
- What are the key features of Spring Boot?
- How do you create a Spring Boot project using Spring Initializr?
- What is the purpose of
application.propertiesin Spring Boot? - How do you change the default port in Spring Boot?
- What are Spring Boot starters?
- What is the difference between
@Component,@Service, and@Repositoryannotations? - How do you handle exceptions in Spring Boot?
- How do you secure a Spring Boot application?
Conclusion
Setting up a Spring Boot project is quick and efficient with tools like Spring Initializr and Spring Boot CLI. With minimal configuration, you can develop Java backend applications efficiently. By following this Spring Boot tutorial, you've created a REST API and configured a database, giving you a solid foundation to build upon.
Got questions? Leave a comment below! 🚀
