visit
@RestController
@RequestMapping("/api")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@ResponseStatus(HttpStatus.OK)
@GetMapping("/employees")
public List<Employee> getEmployees() {
return employeeService.getEmployees();
}
@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/employees")
public Employee createEmployee(@RequestBody EmployeeRequest employee) {
return employeeService.createEmployee(employee);
}
@ResponseStatus(HttpStatus.OK)
@PutMapping("/employees/{employeeId}")
public Employee updatedEmployee(@RequestBody EmployeeRequest employee, @PathVariable int employeeId) {
return employeeService.updateEmployee(employeeId, employee);
}
@ResponseStatus(HttpStatus.NO_CONTENT)
@DeleteMapping("/employees/{employeeId}")
public void deleteEmployee(@PathVariable int employeeId) {
employeeService.deleteEmployee(employeeId);
}
}
git clone [email protected]:jaadds/employee.git
cd employee
./mvnw spring-boot:run
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.1.4)
2023-10-23T15:46:41.186+05:30 INFO 20491 --- [ main] c.example.employee.EmployeeApplication : Starting EmployeeApplication using Java 19.0.2 with PID 20491
2023-10-23T15:46:41.188+05:30 INFO 20491 --- [ main] c.example.employee.EmployeeApplication : No active profile set, falling back to 1 default profile: "default"
2023-10-23T15:46:41.654+05:30 INFO 20491 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-10-23T15:46:41.699+05:30 INFO 20491 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 37 ms. Found 1 JPA repository interfaces.
2023-10-23T15:46:42.166+05:30 INFO 20491 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-10-23T15:46:42.178+05:30 INFO 20491 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
....
curl //localhost:8080/api/employees | jq
[
{
"id": 1,
"firstName": "Bilbo",
"lastName": "Baggins",
"email": "[email protected]"
}
]
Choreo needs certain details to create an endpoint for your service, such as the service port, context path, and service definition. Additionally, you need to specify the endpoint’s visibility level, choosing between public (accessible over the internet) or internal (available only to services running within Choreo). All these details should be defined in the endpoints.yaml file.
For this tutorial, we will expose our employee service as a public endpoint. Create a .choreo/endpoints.yaml file in your project and add the following content:
version: 0.1
endpoints:
- name: Employee API
port: 8080
type: REST
networkVisibility: Public
context: /
schemaFilePath: openapi.yaml
server.port
property.
REST
, GraphQL
, and gRPC
.
Project
, Organization
, and Public
. We'll set this to Public
for this tutorial.
server.servlet.context-path
property, use that value here. Since we haven't changed it, we'll leave it as /
.
./.choreo
folder.
While it’s possible to manually create the OpenAPI Specification (OAS) for a small API like this, Spring’s rich ecosystem provides tools to do it easily. Add the springdoc-openapi dependency to your pom.xml, and start your application.
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
curl //localhost:8080/v3/api-docs.yaml > openapi.yaml
Now that everything is set up, it’s time to deploy our Spring Boot application on Choreo. In case you missed any step, you can cross-check your work with the main branch of. In addition to the steps mentioned here, I’ve updated the snakeyaml dependency to avoid security scan failures.
Before deploying our Spring Boot Application on Choreo, we’ll have to create a component on Choreo. As our Application behaves as a service, we’ll be creating a Service type component.
Field Name | Value |
---|---|
Component Name | Employee Service |
Description | Service for managing employee details |
Organization | Your GitHub account ID |
Repository | The repository containing your code |
Branch |
The branch with the deployable changes (in this case, it’s |
Build Preset | Java |
Java Project Directory | Leave this as is |
Language Version | Select the Java version from your POM (only LTS versions are available). In this case, it’s 17. |
3. Click Create to finalize the component.
Navigate to the Deploy page.
In the Build Area section, verify component details and the latest commit.
Click Configure & Deploy, which will initiate a three-step wizard.
endpoints.yaml
and click Deploy.
Clicking Deploy will initiate the build process. Choreo will compile the source code, build a container out of it, do a vulnerability scan, and deploy it to the development environment. Wait for the status to change to Active.
Go to the Observability section to view logs. You’ll see logs similar to those you saw when running locally.
Finally, we can test the service. The Test section provides a variety of tools to do this. For our case, we will choose the Swagger Console. You may notice that the endpoint you specified in the YAML file appears in the drop-down menu. If you have defined multiple endpoints, all of them will appear in this drop-down.
You can expand on a resource and click Execute.
Congratulations!! You’ve successfully deployed a Spring Boot application on Choreo.
Also published