Spring Boot is a framework that helps to easily develop web applications based on Java. In this tutorial, we will explain in detail how to build a simple backend application using Spring Boot and save it on GitHub. After developing the application, we will guide you through the process of creating a GitHub repository and pushing the code.
Table of Contents
- 1. Introduction to Spring Boot
- 2. Setting Up Development Environment
- 3. Creating a Simple Spring Boot Application
- 4. Creating a GitHub Repository
- 5. Pushing Code
- 6. Conclusion and Additional Resources
1. Introduction to Spring Boot
Spring Boot is a tool provided to build applications quickly and reliably based on the Spring Framework. Spring Boot simplifies configuration and helps developers run applications with minimal effort through auto-configuration.
The main features of Spring Boot are as follows:
- Auto-configuration: Spring Boot automatically handles the basic configuration needed for the project.
- Starter dependencies: Starter dependencies are provided to easily add various features.
- Embedded web server: Servers like Tomcat and Jetty are embedded, allowing applications to run without separate installation.
- Monitoring and management in production: Spring Boot offers various features to monitor and manage applications in production.
2. Setting Up Development Environment
Required Tools Installation
To configure a stable development environment, the following tools are required:
- Java Development Kit (JDK): JDK is necessary to run Spring Boot. You need to install JDK version 8 or higher.
- IntelliJ IDEA or Eclipse: These are IDEs for developing Spring Boot applications. IntelliJ IDEA is available in both free and paid versions, while Eclipse is a free IDE that can be used on all platforms.
- Git: A tool for version control. You can use Git to track code changes and push changes to a remote repository.
- GitHub account: A GitHub account is needed to create a GitHub repository for code storage. You can sign up here.
Installation Procedure for Development Environment
The methods for installing each tool are as follows:
1. Install JDK
The JDK can be downloaded from the Oracle website. Choose the version suitable for your operating system and install it.
2. Install IDE
IntelliJ IDEA can be downloaded from JetBrains’ official website, and Eclipse can be downloaded from the Eclipse website. After installing the IDE, set the JDK path in the IDE.
3. Install Git
Git can be downloaded and installed from the official website. After installation, run the command git --version
in the Command Line or Terminal to verify that the installation is complete.
3. Creating a Simple Spring Boot Application
Now, let’s actually create a Spring Boot application. You can create the application through the following steps.
Step 1: Initialize Spring
To initialize a Spring Boot project, use Spring Initializr.
- Project: Choose (‘Maven Project’ or ‘Gradle Project’)
- Language: Select ‘Java’.
- Spring Boot: Choose the latest Spring Boot version.
- Group: Enter, for example,
com.example
. - Artifact: Enter the desired project name. For example, set it as
my-spring-boot-app
. - Dependencies: Select
Spring Web
. You can add other dependencies as needed.
After finishing the setup, click the Generate
button to download the project as a ZIP file.
Step 2: Import Project in IDE
Import the downloaded ZIP file into the IDE. To do this, open the IDE and select File -> Open
to open the ZIP file, or you can extract the ZIP file and open the folder.
Step 3: Write Basic Code
Once the project structure is set up, open the src/main/java/com/example/myspringbootapp/MySpringBootApp.java
file and update it with the following code:
package com.example.myspringbootapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MySpringBootApp {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApp.class, args);
}
}
This is the starting point of a simple Spring Boot application. Now let’s create a simple REST API.
Step 4: Write REST Controller
First, create a new class file GreetingController.java
. Write it as follows:
package com.example.myspringbootapp;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
@GetMapping("/greeting")
public String greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("Hello, %s!", name);
}
}
The above code creates a /greeting API endpoint that receives a name as a query parameter and returns a greeting message.
Step 5: Run the Application
You can run the main class (MySpringBootApp
) in the IDE or execute the following command in the terminal to start the application:
./mvnw spring-boot:run
You can call the API by entering http://localhost:8080/greeting
in the browser. If you enter http://localhost:8080/greeting?name=Alice
, you will receive the message “Hello, Alice!”.
4. Creating a GitHub Repository
Now it’s time to save the Spring Boot application on GitHub. To upload the code we have created, we first need to create a GitHub repository.
Step 1: Log in to GitHub
First, log in to GitHub. If you don’t have an account, you need to sign up.
Step 2: Create a New Repository
After logging in, click the “+” button at the top right and select “New repository”. Enter the repository name and description, choose either Public
or Private
, and then click the Create repository
button.
5. Pushing Code
Now let’s go through the process of pushing the code you have written locally to the GitHub repository.
Step 1: Initialize Git
Navigate to the project folder and initialize Git. Run the following command in the terminal:
git init
Step 2: Add GitHub Remote Repository
Next, add the address of the repository you created on GitHub. You can copy the repository’s URL from the top URL bar on GitHub:
git remote add origin https://github.com/USERNAME/REPOSITORY_NAME.git
Here, USERNAME
is your GitHub username, and REPOSITORY_NAME
is the repository name you created.
Step 3: Add and Commit Changes
To commit changes, run the following commands:
git add .
git commit -m "Initial commit"
Step 4: Push Code
Finally, push the local changes to GitHub:
git push -u origin master
6. Conclusion and Additional Resources
Congratulations! You have now learned how to create a simple Spring Boot application and how to push the code to a GitHub repository. This process is an important step in laying the foundation for backend development.
You can find additional materials and references through the following links:
I hope this tutorial was helpful. For those wishing to advance further, challenge yourselves to work on various projects utilizing Spring Boot and Git!