Before we start, ensure you have the following prerequisites:
- Java Development Kit (JDK) 8 or higher
- Spring Boot installed via Maven or Gradle
- A preferred Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse, or VS Code
- Basic understanding of Java and RESTful APIs
Follow these steps to build your first microservice:
We will use Spring Initializr to generate the basic structure of our Spring Boot application:
- Navigate to start.spring.io.
- Choose Gradle or Maven as the project type and Java as the language.
- Set the Group and Artifact IDs. For example:
Group:com.example
Artifact:product-service
- Add dependencies: Spring Web, Spring Data JPA, and H2 Database.
- Click Generate to download the project.
Extract the ZIP file and import the project into your IDE.
In a microservice, each service has a specific purpose. Let's create a Product
service for managing product information. Define the following:
public class Product { private Long id; private String name; private double price; // Getters and Setters }
Create a repository interface to manage database interactions:
import org.springframework.data.jpa.repository.JpaRepository; public interface ProductRepository extends JpaRepository<Product, Long> { }
Now, create a service layer to handle business logic:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class ProductService { @Autowired private ProductRepository repository; public List<Product> getAllProducts() { return repository.findAll(); } public Product addProduct(Product product) { return repository.save(product); } }
Expose the service through a REST controller:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/products") public class ProductController { @Autowired private ProductService productService; @GetMapping public List<Product> getAllProducts() { return productService.getAllProducts(); } @PostMapping public Product addProduct(@RequestBody Product product) { return productService.addProduct(product); } }
Spring Boot supports in-memory databases like H2 for quick development and testing. Add the following configuration in your application.properties
file:
spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password=password spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
To test the application:
- Run the Spring Boot application using
mvn spring-boot:run
or./gradlew bootRun
. - Use tools like Postman or
curl
to interact with the endpoints:
curl --location --request POST 'http://localhost:8080/products' \ --header 'Content-Type: application/json' \ --data-raw '{ "name": "Laptop", "price": 999.99 }' curl --location --request GET 'http://localhost:8080/products'
You will see the list of products returned by the service.
To package the application as a JAR, run:
mvn clean package
This will create a JAR file in the target
directory, which you can run independently:
java -jar target/product-service-0.0.1-SNAPSHOT.jar
Congratulations! You have built your first microservice using Spring Boot. This microservice can now be expanded or integrated into a larger system. In subsequent articles, we will explore how to enhance microservices with advanced features like external configuration, communication between services, and security.