How to Create a Microservice with Spring Boot: Simple Guide
To create a microservice with
Spring Boot, start a new project with spring-boot-starter-web dependency, define a @RestController to handle HTTP requests, and run the application as a standalone service. This creates a lightweight, independent service that can communicate over HTTP.Syntax
Here is the basic syntax to create a Spring Boot microservice:
@SpringBootApplication: Marks the main class to enable Spring Boot features.@RestController: Defines a class that handles HTTP requests.@GetMapping: Maps HTTP GET requests to a method.SpringApplication.run(): Starts the Spring Boot application.
java
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class MicroserviceApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceApplication.class, args); } } @RestController class HelloController { @GetMapping("/hello") public String sayHello() { return "Hello from Spring Boot Microservice!"; } }
Example
This example shows a complete Spring Boot microservice that responds with a greeting message when accessed at /hello.
java
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class MicroserviceApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceApplication.class, args); } } @RestController class HelloController { @GetMapping("/hello") public String sayHello() { return "Hello from Spring Boot Microservice!"; } }
Output
When you run this application and visit http://localhost:8080/hello in a browser, you will see:
Hello from Spring Boot Microservice!
Common Pitfalls
Common mistakes when creating Spring Boot microservices include:
- Not adding
spring-boot-starter-webdependency, so the web server won't start. - Forgetting
@RestControllerannotation, so endpoints are not exposed. - Using
@Controllerwithout@ResponseBody, which returns views instead of JSON or text. - Not running the application with
SpringApplication.run().
java
/* Wrong: Missing @RestController, so endpoint won't work */ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; @SpringBootApplication public class MicroserviceApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceApplication.class, args); } } class HelloController { @GetMapping("/hello") public String sayHello() { return "Hello!"; } } /* Right: Add @RestController to expose endpoint */ import org.springframework.web.bind.annotation.RestController; @RestController class HelloController { @GetMapping("/hello") public String sayHello() { return "Hello!"; } }
Quick Reference
Summary tips for creating Spring Boot microservices:
- Use
@SpringBootApplicationon main class. - Add
spring-boot-starter-webdependency for web support. - Define REST endpoints with
@RestControllerand@GetMapping. - Run the app with
SpringApplication.run(). - Test endpoints via browser or tools like curl/Postman.
Key Takeaways
Start a Spring Boot project with spring-boot-starter-web to create a microservice.
Use @RestController and mapping annotations to define HTTP endpoints.
Run your microservice with SpringApplication.run() to start the embedded server.
Test your microservice by accessing its endpoints via HTTP.
Avoid missing annotations or dependencies to prevent common errors.