Hello! Today we will learn how to develop a backend service using Spring Boot and deploy it to Elastic Beanstalk. This course is aimed at those who have an understanding of backend development and AWS services, and we will explain everything step by step from the basics.
Table of Contents
- Introduction to Spring Boot
- Setting Up a Spring Boot Project
- Developing the Backend Service
- Testing the Application
- Creating the Deployment Package
- Introduction to Elastic Beanstalk
- Deploying to Elastic Beanstalk
- Conclusion
Introduction to Spring Boot
Spring Boot is a development framework based on the Spring Framework that helps you develop applications rapidly. Spring Boot enables auto-configuration, provides an embedded server, and allows for easy integration with builders like Maven or Gradle. This allows developers to focus on business logic without the burden of configuration.
Main Features
- Auto-Configuration: Spring Boot automatically configures default settings for the application.
- Standalone: Can run independently using an embedded Tomcat.
- Dependency Management: Supports integration with Maven or Gradle for managing various library dependencies.
- Monitoring: The Actuator allows you to monitor the application’s status.
Setting Up a Spring Boot Project
You can use Spring Initializr to set up a Spring Boot project. On this site, you can enter project metadata and select the necessary dependencies.
1. Accessing Spring Initializr
Access Spring Initializr and input the following:
- Project: Maven Project
- Language: Java
- Spring Boot: 2.5.4 (set to the latest version)
- Group: com.example
- Artifact: demo
- Dependencies: Spring Web, Spring Data JPA, H2 Database
After completing the setup, click the Generate
button to download the ZIP file.
2. Opening the Project in an IDE
Unzip the downloaded ZIP file in your desired directory, and open the project in an IDE like IntelliJ IDEA or Eclipse.
Developing the Backend Service
Now that the project is set up, let’s develop the actual backend service. We will create a simple user management REST API.
1. Creating the Entity
Create a User
entity to store user information.
package com.example.demo.model;
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;
private String email;
// Getters and Setters
}
2. Creating the Repository Interface
Create a repository interface for interacting with the database using JPA.
package com.example.demo.repository;
import com.example.demo.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository {
}
3. Creating the Service Class
Create a service class to handle business logic.
package com.example.demo.service;
import com.example.demo.model.User;
import com.example.demo.repository.UserRepository;
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);
}
}
4. Creating the Controller Class
Create a controller to handle REST API endpoints.
package com.example.demo.controller;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/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);
}
}
Testing the Application
Now let’s test if the application is running well. After running the Spring Boot application, you can call the REST API using Postman or cURL.
1. Running the Application
Start the application by running the main class in the IDE. It will run at http://localhost:8080
by default.
2. User Creation Request
Open Postman and create a request to call the user creation API as follows.
- POST:
http://localhost:8080/api/users
- Body (in JSON format):
{ "name": "John Doe", "email": "john@example.com" }
3. User Retrieval Request
Create a GET request to retrieve all user information.
- GET:
http://localhost:8080/api/users
Creating the Deployment Package
If the application works correctly, create a package for deployment. We will build the JAR file using Maven.
1. Maven Build
Run the following command in the terminal to build the project.
./mvnw clean package
This command generates a JAR file in the target
folder.
Introduction to Elastic Beanstalk
Elastic Beanstalk, provided by Amazon Web Services (AWS), is a service that helps deploy and manage web applications easily. Using this service, AWS handles the server and infrastructure management, allowing developers to focus solely on application development.
Main Features
- Automated Infrastructure Management: Automatically manages all infrastructure including servers, load balancers, and databases.
- Stability: Provides a reliable hosting environment using EC2 instances.
- Scaling: Automatically adds instances as the number of users increases.
- Monitoring: Allows real-time monitoring of application status and logs through the AWS Management Console.
Deploying to Elastic Beanstalk
Now we will go through the process of deploying the created JAR file to Elastic Beanstalk.
1. Creating and Logging into an AWS Account
First, create an AWS account and log into the console.
2. Creating an Elastic Beanstalk Environment
Select Elastic Beanstalk in the AWS Management Console, and click the Create New Application
button. Enter the application name and description, and click Create
.
3. Creating an Environment
In the application dashboard, click Create New Environment
, and then choose Web server environment
. Select Java
as the Platform, choose Upload your code
for the application code, and upload the previously created JAR file.
4. Configuring the Environment
After completing the settings for the environment, click Create Environment
to finalize the environment creation. This process may take a few minutes.
5. Accessing the Application
After the environment is created, you can access the deployed application’s URL. Visit that URL to check if the application is functioning properly.
Conclusion
In conclusion, we have learned how to develop a backend service using Spring Boot and deploy it to Elastic Beanstalk. Thanks to the powerful features of Spring Boot and AWS’s flexible infrastructure management, quick and stable development and deployment have become possible. I recommend operating various services through AWS in the future.
Thank you!