Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 94

    Enterprise API Design

    Enterprise APIs are contracts that outlast their authors.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Enterprise APIs are contracts that outlast their authors. TechLearningPRO Orders must treat HTTP as a product: versioned, documented with OpenAPI, monitored, and deprecated with sunset dates — not a pile of ad-hoc controllers.

    On Boot 3, generate OpenAPI from code (springdoc-openapi), return RFC 7807 Problem Details for errors, require idempotency keys on writes, and standardise pagination/filtering. Partners need rate-limit headers and signed webhooks with retries.

    Plan deprecation before launching v1. Breaking changes without a parallel version strand mobile clients and B2B integrators.

    This lesson productises the Orders API before scalable-system invariants.

    Purpose of this lesson

    Design Orders as a versioned, documented, idempotent enterprise API with Problem Details and partner-safe evolution.

    Understanding the topic

    Enterprise checklist for long-lived HTTP contracts.

    • OpenAPI 3.1: generated from code (springdoc-openapi).
    • Versioning: path /v1 + deprecation and sunset headers.
    • Errors: RFC 7807 Problem JSON.
    • Idempotency keys: on all creates/updates that must not double-charge.
    • Lists: pagination, filtering, sorting conventions.
    • Sparse fieldsets: when resources are large.
    • Webhooks: HMAC signatures + retries for outbound events.
    • SLA headers: X-RateLimit-* (or standard equivalents).

    Step-by-step explanation

    1. Add springdoc and publish /v3/api-docs for Orders.
    2. Adopt Problem Details for 4xx/5xx; include a stable type URI.
    3. Require Idempotency-Key on POST /api/v1/orders; store key → response.
    4. Document deprecation policy; add rate-limit headers at the gateway.

    Informative example

    Problem Details + idempotent place-order (Boot 3).

    java
    @RestControllerAdvice
    public class ApiExceptionHandler {
    @ExceptionHandler(OrderConflictException.class)
    ResponseEntity<ProblemDetail> conflict(OrderConflictException ex) {
    ProblemDetail pd = ProblemDetail.forStatus(HttpStatus.CONFLICT);
    pd.setTitle("Order conflict");
    pd.setDetail(ex.getMessage());
    pd.setType(URI.create("https://api.techlearningpro.com/problems/order-conflict"));
    pd.setProperty("orderId", ex.getOrderId());
    return ResponseEntity.status(HttpStatus.CONFLICT).body(pd);
    }
    }
    @PostMapping("/api/v1/orders")
    ResponseEntity<OrderResponse> place(
    @RequestHeader("Idempotency-Key") String idemKey,
    @Valid @RequestBody CreateOrderRequest body) {
    return ResponseEntity.status(201).body(orders.placeIdempotent(idemKey, body));
    }

    Real-world use

    A partner retried POST /orders on a gateway timeout and created duplicate charges. TechLearningPRO added mandatory idempotency keys with a 24h retention table — duplicates returned the original 201 body, and support tickets dropped overnight.

    Security implications

    API product design is security design.

    • Authenticate partners; rotate webhook HMAC secrets via a secret store.
    • Do not put secrets in URLs or error details.
    • Rate-limit and authz every versioned route — old versions stay patched.

    Best practices

    • Generate OpenAPI from annotations/code; fail CI on breaking changes.
    • Paginate every list; never return unbounded collections.
    • Design webhooks with signature verification and exponential retry.

    Common mistakes

    • Breaking JSON fields without a new version or sunset period.
    • Ad-hoc error shapes per controller.
    • Documenting OpenAPI by hand until it drifts from code.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionWhy use RFC 7807 Problem Details?+

    Answer

    A standard error shape clients can parse consistently across endpoints and services.
    2IntermediateQuestionWhat do idempotency keys prevent?+

    Answer

    Duplicate side effects when clients retry after timeouts or network failures.
    3IntermediateQuestionHow should you deprecate an API field?+

    Answer

    Announce with headers/docs, keep behaviour for a sunset window, then remove in a new major version.

    Hands-on exercise

    Lab. Version Orders under /api/v1, return ProblemDetail on validation failure, and implement idempotency for place-order. Replay the same key and assert one DB row.

    Summary

    Enterprise API design keeps Orders stable for partners. Next: the invariants of scalable backend systems.

    Ready to mark this lesson complete?Track your journey across the entire course.