# Create a Spring Boot Microservice

This tutorial uses the Spring Initializr to create a simple microservice to quickly generate an Entando bundle project.

# Prerequisites

# Step 1: Initialize the Bundle with a Microservice

  1. Initialize a new bundle:
ent bundle init e71-spring-project
  1. From the bundle project root directory, create a new microservice:
cd e71-spring-project
ent bundle ms add spring-ms

# Step 2: Create the Microservice

  1. 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=2.7.6
	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=11
	Dependencies:
	             #under WEB: Spring Web 
	             #under OPS: Spring Boot Actuator

Click generate.

  1. Unzip the package and move the unzipped files and src directory to the microservices/spring-ms/ directory.

  2. Create a new directory for the controller:

mkdir microservices/spring-ms/src/main/java/com/entando/example/springms/controller
  1. 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; }
         }
}
  1. 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
  1. Run the microservice from the bundle project root directory to test that it works:
ent bundle run spring-ms
  1. Create microservices/spring-ms/Dockerfile so ent knows how to assemble the Docker image for the service:
FROM openjdk:11
WORKDIR /app
COPY target/*.jar /app/app.jar
CMD ["java", "-jar", "app.jar"]
EXPOSE 8081

# Step 3: Build and Install the Bundle

  1. Optionally, add an image file in PNG or JPG format named thumbnail for the project.

  2. To install your bundle, execute the following commands:

ent bundle pack
ent bundle publish
ent bundle deploy
ent bundle install 
  1. To test the installed microservice, fetch your host name and microservice path with this command:
ent kubectl describe ingress 

Next Steps