This project demonstrates how to implement a basic webhook mechanism using Spring Boot. The core concept of a webhook is to allow one application to notify another application about specific events by invoking an API.
For example, in a payment system, we might configure a webhook to call an external API once the payment is successfully processed. Similarly, in this project, when a student is added to a school (in the main application), a corresponding API in the hook application is triggered.
The system consists of two independent Spring Boot applications:
- Main Application (
main
) – Handles core operations such as creating schools, registering webhook details, and adding students. - Hook Application (
hook
) – Represents a specific school's server. It exposes an endpoint that is triggered by the main application via a webhook call.
-
Create a School:
A new school (e.g.,XYZ School
) is created via the main application. -
Register Webhook Details:
Webhook configuration is set for a school. This includes:eventName
– Type of event (e.g.,add
,delete
,update
).endpointUrl
– The URL of the endpoint in the hook application that should be called when the event occurs.
Webhook details are stored in a table and linked to the corresponding school by
schoolId
. -
Setup Hook Application:
The hook application simulates the server of a specific school. It exposes an endpoint (e.g.,/student-added/{name}
) that simply logs the student's name when triggered. -
Add a Student (Main Application):
When a student is added to a school, the main application:- Accepts
schoolId
,studentName
, andage
as parameters. - Looks up webhook details based on
schoolId
andeventName
=add
. - Sends a request to the configured endpoint in the hook application, appending the
studentName
.
- Accepts
-
Trigger Webhook (Hook Application):
The hook application receives the request and prints the student name to the console.
⚠️ Ensure that both applications are running on different ports.
This project demonstrates how to implement a webhook integration between two Spring Boot applications. The flow is as follows:
- A school is created in the main application.
- Webhook details for the school are configured and linked by
schoolId
. - The hook application acts as the server for the school and exposes a handler for student-related events.
- When a student is added via the main app, a webhook is triggered and the corresponding endpoint in the hook app is called dynamically.
This architecture allows seamless event-based communication between distributed services.