π Setting Up a Simple Spring Boot Microservice in GitHub Codespaces
In this guide, we'll create a "Hello World" Spring Boot microservice and run it in GitHub Codespaces. This is a great way to quickly spin up and experiment with microservices in a cloud-based development environment. π
Letβs dive in! π
π‘ What You Will Achieve
You will learn how to:
-
Set up a Spring Boot project in GitHub Codespaces.
-
Write a simple REST API that returns a "Hello World" message.
-
Run and test the application.
1οΈβ£ Create a New Repository in GitHub
-
Go to your GitHub account and create a new repository.
-
Name it something like
spring-boot-hello-world. π -
Make sure to initialize it with a README.md.
(Pause here to take a screenshot of creating a new repo!)
2οΈβ£ Launch GitHub Codespaces
-
Once the repository is created, click on the Codespaces tab.
-
Create a new Codespace instance by clicking the green New codespace button. π
(Pause here to take a screenshot of launching Codespaces!)
3οΈβ£ Configure Codespaces Environment
-
When the Codespace opens, youβll need to ensure Java and Maven are installed.
-
To automate this setup, you can add a
.devcontainer/devcontainer.jsonfile:
{
"name": "Java Dev Environment",
"image": "mcr.microsoft.com/devcontainers/java:0-17",
"customizations": {
"vscode": {
"extensions": [
"vscjava.vscode-java-pack"
]
}
},
"postCreateCommand": "mvn clean install"
}
This will automatically install the necessary environment, so you can focus on coding! π»
4οΈβ£ Create the Spring Boot Project
-
You can use Spring Initializr by visiting Spring Initializr and selecting:
-
Maven Project.
-
Java Language.
-
Spring Boot version 2.7.x or 3.x.
-
Add Spring Web as a dependency.
Alternatively, you can run the following command directly in the Codespace terminal:
curl https://start.spring.io/starter.zip \
-d dependencies=web \
-d language=java \
-d javaVersion=17 \
-o spring-boot-hello-world.zip
unzip spring-boot-hello-world.zip -d .
5οΈβ£ Implement the "Hello World" Microservice
Navigate to src/main/java/com/example/demo and create the following files:
DemoApplication.java
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);
}
}
HelloWorldController.java
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@GetMapping("/hello")
public String sayHello() {
return "Hello World";
}
}
6οΈβ£ Run the Application in Codespaces
Now that everything is set up, you can run the application by using the following command in the terminal:
./mvnw spring-boot:run
7οΈβ£ Test the Application
Once the application is running, open a new terminal and test the endpoint using curl or your browser:
curl http://localhost:8080/hello
You should see the response:
Hello World
π Congratulations! Youβve just built and deployed a Spring Boot microservice!
8οΈβ£ Commit and Push Changes
After confirming that everything works, commit and push your changes back to GitHub:
git add .
git commit -m "Initial Hello World microservice"
git push origin main
π‘ Thatβs it! Youβve successfully set up and deployed a Spring Boot project in GitHub Codespaces. Now, you can build on this foundation to create more advanced microservices. π
π Connect with Me:
Letβs build something amazing together! β¨
Imported from rifaterdemsahin.com Β· 2024