There is no standard @Webhook annotation in Spring, Jakarta EE, Stripe, PayPal or any other major payment SDK. Examples that use one are either a custom annotation written by a team or code that was invented. A webhook is simply an HTTP endpoint that your application exposes so a payment provider can notify it of events such as a completed payment, and in Spring you build it with an ordinary @RestController and @PostMapping.
What the question really asks is how to receive payment webhooks correctly. That comes down to four things: read the raw request body, verify the signature, process each event idempotently, and answer with a fast 2xx response.
@Webhook annotationA webhook is a reverse API call. Instead of your server polling the provider to ask whether a payment succeeded, the provider sends an HTTP POST with a JSON event to a URL you registered. Payments are asynchronous by nature: a card may need 3-D Secure, a bank debit may settle days later, and a subscription renews without any user present. Webhooks are how your system learns about those outcomes.
Typical events are a payment succeeding or failing, a refund being created, a dispute opening, or a subscription being cancelled. You should treat the webhook, not the browser redirect after checkout, as the source of truth for fulfilling an order.
@Webhook annotationSearch the Spring, Stripe and PayPal documentation and you will not find @Webhook. The mechanism is plain Spring MVC:
| You want | Use |
|---|---|
| Expose the endpoint | @RestController with @PostMapping("/webhooks/stripe") |
| Read the raw payload | @RequestBody String payload |
| Read the signature header | @RequestHeader("Stripe-Signature") String signature |
| Reject bad requests | ResponseEntity with 400 or 401 |
If you like the declarative style, you can define your own annotation and an argument resolver or interceptor that performs signature checks. That is a convenience you write yourself, not a framework feature, so document it as such.
Stripe signs each request with the Stripe-Signature header. The Stripe Java library verifies it for you, but it needs the exact raw body, so do not bind the payload to a Java object first.
The endpoint secret comes from the provider dashboard and must live in configuration or a secrets manager, never in source control. Other providers follow the same pattern with different header names and algorithms, so read their verification guide.
If a provider has no SDK, the common scheme is an HMAC-SHA256 of the raw body using a shared secret. Always compare in constant time.
MessageDigest.isEqual avoids timing leaks that a plain equals call would introduce. Many providers also include a timestamp in the signed data; reject events older than a few minutes to block replay attacks.
Providers retry any delivery that does not get a 2xx response, so the same event can arrive more than once, and events can arrive out of order. Store the event id and skip ones you have already handled.
Back insertIfAbsent with a unique constraint on the event id, so two concurrent deliveries cannot both succeed. For slow work, store the event and hand it to a queue, then return 200 immediately; providers time out after a few seconds.
400 for bad signatures and 5xx only for failures you want retried.@Webhook part of Spring or Stripe?No. Neither Spring nor the major payment SDKs define it. Use @PostMapping on a controller method, or write your own annotation if you want a declarative style.
The signature is computed over the exact bytes the provider sent. If you parse the JSON and serialise it again, whitespace or field order can change and verification fails.
Return 200 or 204 as soon as the event is stored or queued. A slow or failing response makes the provider retry, which multiplies duplicates.
Use the provider's CLI to forward events to localhost, for example the Stripe CLI listen command, and keep a test secret in your local profile.
Payment webhooks are ordinary authenticated-by-signature HTTP endpoints, and no @Webhook annotation exists to hide that. Receive the raw body in a @PostMapping method, verify the signature, process each event id once, and respond quickly with 2xx. Treat the webhook as the source of truth for fulfilment, and keep secrets in configuration.