Step 1: Create a Spring Boot Application
- 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
- Group:
- Dependencies: Add Spring Web
- Click on “Generate” to download the project.
- Extract the downloaded project: Extract the zip file and navigate to the project directory.
- Open the project in your favorite IDE (e.g., IntelliJ IDEA, Eclipse, VSCode).
- Create a simple REST controller:
- Open
src/main/java/com/example/demo/DemoApplication.java
and ensure it looks like this:
- Open
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
- Initialize a Git repository:Open a terminal in the project directory and run:
git init
git add .
git commit -m "Initial commit"
- 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”.
- 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.git
git branch -M main
git 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