Creating and Deploying a Simple Spring Boot Application to GitHub: A Step-by-Step Guide

Step 1: Create a Spring Boot Application

  1. Generate a new Spring Boot project: You can use the Spring Initializr to generate a new project. Go to start.spring.io and configure your project:
    • Project: Maven Project
    • Language: Java
    • Spring Boot: 3.0.0 (or the latest stable version)
    • Project Metadata:
      • Group: com.example
      • Artifact: demo
    • Dependencies: Add Spring Web
    • Click on “Generate” to download the project.
  2. Extract the downloaded project: Extract the zip file and navigate to the project directory.
  3. Open the project in your favorite IDE (e.g., IntelliJ IDEA, Eclipse, VSCode).
  4. Create a simple REST controller:
    • Open src/main/java/com/example/demo/DemoApplication.java and ensure it looks like this:
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);
    }

}

Create a new file src/main/java/com/example/demo/HelloController.java and add the following content:

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 hello() {
        return "Hello, World!";
    }
}

Build the project: Open a terminal in the project directory and run:

./mvnw clean install

Run the application: Run the application using the command:

./mvnw spring-boot:run

You should be able to access http://localhost:8080/hello and see “Hello, World!”.

Step 2: Save the Project to GitHub

  1. Initialize a Git repository:Open a terminal in the project directory and run:
git initgit add . git commit -m "Initial commit"
  1. Create a new repository on GitHub:
    • Go to GitHub and log in to your account.
    • Click on the “+” icon in the top right corner and select “New repository”.
    • Fill in the repository name (e.g., spring-boot-demo) and description.
    • Set the repository to public or private.
    • Click “Create repository”.
  2. Push the local repository to GitHub:Follow the instructions provided by GitHub to push your local repository. Usually, the commands will be similar to:
git remote add origin https://github.com/<your-username>/spring-boot-demo.gitgit branch -M maingit push -u origin main

Summary

You’ve created a simple Spring Boot application, initialized a Git repository, and pushed it to GitHub. Now, you can share your project or collaborate with others using GitHub.

Leave a Reply

Your email address will not be published. Required fields are marked *