# Create a Spring Boot Microservice
This tutorial uses the Spring Initializr to create a simple microservice to quickly generate an Entando bundle project.
# Prerequisites
- A working instance of Entando
- Verify dependencies with the Entando CLI:
ent check-env develop
# Step 1: Initialize the Bundle with a Microservice
- Initialize a new bundle:
ent bundle init your-spring-project
Copied!
- From the bundle project root directory, create a new microservice:
cd your-spring-project ent bundle ms add spring-ms
Copied!
# Step 2: Create the Microservice
- At the Spring Initialyzr page, start.spring.io (opens new window), create a project with the following configuration:
Project=Maven Language=Java Spring Boot version=3.2.3 Group=com.entando.example Artifact=spring-ms Name=spring-ms Description=Demo project for Spring Boot Package name=com.entando.example.spring-ms Packaging=Jar Java=17 Dependencies: WEB: Spring Web OPS: Spring Boot Actuator
Copied!
Click generate.
Unzip the package and move the unzipped files and
src
directory to themicroservices/spring-ms/
directory.Create a new directory for the controller:
mkdir microservices/spring-ms/src/main/java/com/entando/example/springms/controller
Copied!
- In the controller directory, create
TemplateController.java
with the following code:
package com.entando.example.springms.controller; import org.springframework.web.bind.annotation.*; @RestController public class TemplateController { @CrossOrigin @GetMapping("/api/example") public MyResponse getExample() { return new MyResponse("test Data"); } public static class MyResponse{ private final String payload; public MyResponse(String payload) { this.payload = payload; } public String getPayload() { return payload; } } }
Copied!
- To make the microservice compatible with Entando, add the following snippet to the
microservices/spring-ms/src/main/resources/application.properties
file:
server.port=8081 management.endpoints.web.base-path=/api
Copied!
- Run the microservice from the bundle project root directory to test that it works:
ent bundle run spring-ms
Copied!
- In your browser, access http://localhost:8081/api/example (opens new window) to see
{"payload":"test Data"}
. - Then, access http://localhost:8081/api/health (opens new window) to see
{"status":"UP"}
. In local development, the run command can be used to modify the ports to run multiple microservices, but in production, microservices must run on port 8081.
- Create
microservices/spring-ms/Dockerfile
so ent knows how to assemble the Docker image for the service:
FROM openjdk:17 WORKDIR /app COPY target/*.jar /app/app.jar CMD ["java", "-jar", "app.jar"] EXPOSE 8081
Copied!
# Step 3: Build and Install the Bundle
Optionally, add an image file in PNG or JPG format named
thumbnail
for the project.To install your bundle, execute the following commands:
ent bundle pack
Copied!
ent bundle publish
Copied!
ent bundle deploy
Copied!
ent bundle install
Copied!
- Test your Spring Boot microservice:
- Retrieve
YOUR-HOST-NAME
andYOUR-MS-PATH
with this command:
ent kubectl describe ingress
Copied!
YOUR-MS-PATH
is listed under path
and should be similar to this:
/your-spring-project-83fbf4bf/spring-ms
Copied!
- Using your values, go to
YOUR-HOST-NAME
/YOUR-MS-PATH
/api/example/ in your browser. It should return{"payload":"test Data"}
.
E.g., for a quickstart project,
URL=
http://quickstart.192.168.64.34.nip.io/your-spring-project-83fbf4bf/spring-ms/api/example/
Next Steps
- Learn to connect micro frontends to microservices by adding an API Claim.
- Add a configuration micro frontend to your bundle project.