This course covers how to set up a backend development environment using Spring Boot and how to structure projects in an understandable way. This course is aimed at beginners and is explained step by step, making it easy to follow.
1. What is Spring Boot?
Spring Boot is a Java-based framework that provides tools to help developers create Spring applications quickly and easily. It reduces the complexity of the traditional Spring framework, simplifies configurations, and provides a variety of ready-to-use features.
1.1 Key Features
- Auto-configuration: Automatically configures necessary settings.
- Standalone: Can be run without a separate server installation.
- Modularity: Can be structured into individual modules that include only the necessary dependencies.
- Embedded Server Support: Supports embedded servers like Tomcat, Jetty, and Undertow.
2. Setting Up the Development Environment
To use Spring Boot, you need to set up the development environment. Let’s prepare the development environment by following the steps below.
2.1 Prerequisites
- Install Java Development Kit (JDK) version 11 or higher.
- Install an IDE (e.g., IntelliJ IDEA, Eclipse).
- Install Maven or Gradle (e.g., Maven helps with Dependency Management).
2.2 Installing JDK
To install the JDK, download the installer from either the Oracle or OpenJDK website and follow the instructions. After installation, check if the JDK is installed properly by entering the following command in the command prompt or terminal:
java -version
2.3 Installing IDE
Install an IDE such as IntelliJ IDEA or Eclipse. This guide describes how to use IntelliJ IDEA. Download and install the ‘Community Edition’ from the official website.
2.4 Installing Maven
Maven is a tool for creating Spring Boot projects and managing dependencies. Download and install it from the official Maven website.
3. Creating Your First Spring Boot Project
Now that the development environment is ready, let’s talk about how to create and run a Spring Boot project.
3.1 Using Spring Initializr
You can use Spring Initializr to easily set up a project using Spring Boot. Access Spring Initializr in your browser.
- Project: Select Maven Project
- Language: Select Java
- Spring Boot: Choose the version you want to use (latest stable version recommended)
- Project Metadata:
- Group: com.example
- Artifact: demo
- Name: demo
- Description: Demo project for Spring Boot
- Package name: com.example.demo
- Packaging: Select Jar
- Java: Select 11
- Dependencies:
- Spring Web
- Spring Data JPA
- H2 Database
Once you’ve completed the configuration, click the ‘Generate’ button to download the project. Extract the downloaded zip file and save it in your desired location.
3.2 Opening the Project in IDE
Run IntelliJ IDEA, then click the ‘Open’ button and select the project folder you just extracted. Once the IDE loads the project, it will download the necessary dependencies and complete the initial setup.
4. Understanding Project Structure
Once the project is created, a basic folder structure is established. Understanding the role of each folder will greatly help in future development.
4.1 Main Folders and Files
- src/main/java: This folder contains the main source code of the application.
- src/main/resources: This folder contains configuration files (yaml, properties) and static resources.
- src/test/java: This folder contains test code.
- pom.xml: This is the configuration file for the Maven project, managing dependencies and plugins.
5. Creating a Simple RESTful API
Now let’s create a simple RESTful API. For example, we will create a simple controller that returns ‘Hello, World!’.
5.1 Creating the Controller
Create a file named HelloController.java in the src/main/java/com/example/demo package and write the following code:
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, World!";
}
}
5.2 Running the Application
Open the main application file (DemoApplication.java) and run the code below to start the server:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Once the server is running, you can access http://localhost:8080/hello in a browser to see the ‘Hello, World!’ message.
6. Integrating with the Database
Now let’s integrate the H2 database and implement simple CRUD functionality. The H2 database is an in-memory database suitable for development and testing purposes.
6.1 Adding H2 Database Dependency
Add the H2 database dependency in the pom.xml file:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
6.2 Creating the Entity Class
Create an entity class that will be mapped to the database table. Create a file named User.java in the src/main/java/com/example/demo package:
package com.example.demo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
// Getters and Setters
}
6.3 Creating the Repository Interface
Create a JPA repository interface for interacting with the database. Create a file named UserRepository.java in the src/main/java/com/example/demo package:
package com.example.demo;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository {
}
6.4 Creating the Service Class
Create a service class that handles business logic. Create a file named UserService.java in the src/main/java/com/example/demo package:
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public List getAllUsers() {
return userRepository.findAll();
}
public User createUser(User user) {
return userRepository.save(user);
}
}
6.5 Updating the Controller
Now, let’s update the HelloController to implement new features. Modify the HelloController.java in the src/main/java/com/example/demo package to add user creation and retrieval functionality:
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public List getUsers() {
return userService.getAllUsers();
}
@PostMapping
public User createUser(@RequestBody User user) {
return userService.createUser(user);
}
}
6.6 Testing
Use Postman or cURL to call the API and test the CRUD functionality. Verify that user information is saved and retrieved from the database.
7. Conclusion and Next Steps
This concludes the simple backend development course using Spring Boot. I hope you have learned the basic concepts of Spring Boot, how to set up a project, create a RESTful API, and how to integrate with a database through this course.
In the next course, we will cover advanced topics such as JWT-based authentication and authorization, custom exception handling, and writing tests. Continue to explore the world of Spring Boot!