Spring Boot Backend Development Course, Git and GitHub

Hello! In this tutorial, we will explore how to utilize Git and GitHub while starting Spring Boot development. Let’s understand the importance of source code management and version control, and learn how to apply them in actual projects.

1. What is Spring Boot?

Spring Boot is a sub-project of the Spring framework, which is a Java-based web application development framework, and it helps to develop web applications faster and more easily. Its main features include:

  • Auto-configuration: Spring Boot automatically configures necessary settings without the need for the developer to define a configuration file.
  • Integration with the Spring ecosystem: Spring Boot can be easily integrated with various Spring projects (Spring MVC, Spring Data, etc.).
  • Standalone: It can be distributed as a standalone JAR file without requiring a separate web server when deployed on a server.

2. What are Git and GitHub?

2.1 Git

Git is a distributed version control system that records the history of code changes and allows multiple developers to work simultaneously. Git has several advantages, including:

  • Fast performance: All operations are performed locally, leading to smooth software performance.
  • Small size: The size of the repository is kept small, allowing effective management of multiple versions of code.
  • Distributed: Each developer’s local repository is managed independently, so it does not rely on a central server.

2.2 GitHub

GitHub is a platform that helps manage source code using Git, allowing for collaboration and public sharing. Its main features include:

  • Code hosting: You can store and manage project code in the cloud.
  • Issue tracking: It provides useful tools for tracking and managing bugs or improvement requests.
  • Collaboration: It enables multiple developers to work on the same project simultaneously.

3. Setting up the environment

3.1 Installing Java

To use Spring Boot, you need to install the Java Development Kit (JDK). You can install it from Oracle’s official website or OpenJDK.

sudo apt update
sudo apt install openjdk-11-jdk
java -version

3.2 Installing Spring Boot CLI

Using the Spring Boot CLI (Command Line Interface), you can easily create Spring Boot applications. The installation steps are as follows:

brew tap spring-io/tap
brew install springboot

3.3 Installing Git

If Git is not yet installed, you can install it using the following command:

sudo apt install git

3.4 Creating a GitHub account

Sign up for GitHub and create an account. Just go to the official GitHub website to sign up.

4. Creating a simple Spring Boot application

Now, let’s create a Spring Boot application. Use the following command to create a new project:

spring init --dependencies=web my-spring-boot-app

Once the project is created, navigate to the directory.

cd my-spring-boot-app

4.1 Creating a simple REST API

Let’s write a simple example that provides data using a REST API in a Spring Boot application. Create a file named `src/main/java/com/example/myapp/MyController.java` and write the following code:

package com.example.myapp;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {
    @GetMapping("/hello")
    public String hello() {
        return "Hello, Spring Boot!";
    }
}

Now, let’s run the application. You can execute the following command to run the application:

./mvnw spring-boot:run

After running, check the result by accessing http://localhost:8080/hello in your web browser, where you should see the message “Hello, Spring Boot!”.

5. Version control using Git and GitHub

5.1 Initializing Git

Now, let’s initialize Git in the project. Move to the project folder and enter the following command:

git init

Executing this command will create a .git directory in the current directory, initializing a Git repository.

5.2 Adding files and committing

Let’s add files and commit. Use the following command to add all files to the staging area:

git add .

Then proceed with a commit.

git commit -m "Initial commit: Created Spring Boot application"

5.3 Creating a repository on GitHub

Go to GitHub and create a new repository. After creating the new repository, add the remote repository.

git remote add origin https://github.com/username/my-spring-boot-app.git

5.4 Pushing to the remote repository

Now, let’s push the project to GitHub. Push using the following command:

git push -u origin master

Now you can check the project code on GitHub.

6. Utilizing GitHub for team collaboration

As the project grows, you will collaborate with multiple team members, and you can utilize various collaboration features of GitHub at this time. There are many features, including merge requests (Pull Requests), code reviews, and issue management.

6.1 Issue management

Create issues to manage bugs or requests. Issues allow team members to share opinions and assign tasks.

6.2 Code review

After pushing the code, you can request a review from team members. Reviews help improve code quality and receive various feedback.

6.3 Merge requests (Pull Request)

After developing a new feature, use a pull request to merge that feature into the main branch. This option allows other team members to review and approve the changes.

7. Conclusion

In this tutorial, we created a simple backend application using Spring Boot and explored source code management and collaboration methods using Git and GitHub. Through this process, we hope you gain a basic understanding of Spring Boot and Git.

To further enhance your skills, search for additional resources and apply them to various projects. In the next tutorial, we will delve deeper into Spring Boot’s JSON processing, configuration management, and more.

References