← Back to Blog

πŸš€ Setting Up a Simple Spring Boot Microservice in GitHub Codespaces

πŸš€ 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.

πŸš€ 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.json file:

{
"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