REST API design with Spring
41 questions in 10 subtopics.
Resource modelling
What isProblemDetail in Spring Framework 6 and how does it relate to error responses in a REST API?You are designing endpoints for an order system in Spring MVC. A colleague proposes POST /createOrder, GET /getOrder?id=5 and POST /cancelOrder?id=5. How would you model these as REST resources, and which HTTP methods and status codes would you use?Write a Spring MVC controller method that creates a customer from a JSON body, validates it, and returns 201 with a Location header. What annotations and return type do you use?A mobile team complains that rendering one screen needs seven calls to your REST API (user, orders, addresses, and so on). How do you decide between adding an aggregate endpoint, supporting field selection or embedding, introducing a backend-for-frontend, or moving to GraphQL?Design the REST resources for a bank transfer between two accounts, including how a client learns the transfer's outcome when processing takes several seconds. Where would you use synchronous responses and where asynchronous ones?You lead API standards for 40 teams whose REST APIs differ in naming, error formats and pagination. What would you put in a company API guideline, and how would you get adoption without blocking delivery?Over ten years your company built several hundred internal REST APIs with inconsistent models of the same business concepts (customer, account, product). What long-term strategy would you set for the shape of those APIs and the data concepts behind them?Versioning
What are the common ways to version a REST API (URI path, header, query parameter, media type), and what are the practical differences between them?In Spring Framework 7 and Spring Boot 4 you need GET /accounts/{id} to serve two response shapes depending on an API-Version request header. How can you implement this with the framework's versioning support, and how would you do it on an older Spring version?Define a versioning and deprecation policy for public and internal REST APIs across a company of many teams. What counts as a breaking change, how long do old versions live, and how is retirement enforced?Your company supports five major versions of its public API, and each new feature must be back-ported to several of them. Engineering cost keeps rising. How would you change versioning strategy for the next five years?Pagination and sorting
A Spring MVC endpoint returnsPage<ProductDto> directly from a controller using Spring Data's Pageable argument. What does the JSON look like, what problems can that cause, and what does newer Spring Data recommend?Users report that paging through a list of orders sorted by status sometimes shows the same order on two pages and skips others. The query is ORDER BY status LIMIT 20 OFFSET :offset. What is the cause and the fix?An endpoint GET /events?page=5000&size=50 on a table with 200 million rows has become slow. Explain why offset pagination degrades and design a keyset (cursor) pagination API in Spring, including the SQL and the cursor format.Set a company standard for pagination, filtering and sorting in REST APIs used by web, mobile and partner clients. What would the standard specify and why?Ten years ago your platform's public API exposed offset pagination with exact total counts on every list endpoint. Now the counts dominate database load. How do you reason about the cost of that early decision and what do you do now?Idempotency
Which HTTP methods are idempotent by definition, and why does idempotency matter for a client that retries after a timeout?Design idempotent POST /payments in a Spring Boot service using an Idempotency-Key header. Cover storage, concurrent duplicates, what is returned on replay, and expiry.Several teams retry calls between services with different policies, and a recent incident created duplicate shipments because the shipping service treats every POST as new. What cross-team standard would you set for idempotency?How would you argue for investment in end-to-end idempotency (APIs, messaging, payments) to leadership that sees it as engineering overhead?HATEOAS
What is HATEOAS, and how does Spring HATEOAS help you add links to a response for GET /orders/{id}?Your team debates adopting HATEOAS for an internal API consumed by three known services. Most client code hardcodes URLs anyway. What would you recommend and why?A partner-facing workflow API (application submitted, under review, approved, rejected) changes its allowed transitions every quarter. How could hypermedia controls reduce the coordination cost with partners, and what would you need in place for it to work?HATEOAS has been recommended for two decades but few public APIs use it fully. What does that history tell you when evaluating architectural ideals for your organisation's API strategy?OpenAPI and springdoc
How does springdoc-openapi generate an OpenAPI document from a Spring Boot application, and how do you improve the generated description of an endpoint?Your team must choose between code-first (springdoc generates the spec from controllers) and design-first (write OpenAPI YAML, generate server interfaces with openapi-generator). What are the trade-offs and what would you pick for a partner API?How would you build an API catalog and governance process based on OpenAPI documents for 200 services, so that consumers can find APIs and breaking changes are caught before release?You are deciding whether machine-readable API contracts (OpenAPI, AsyncAPI, protobuf) should be mandatory for every service in the company. What long-term benefits and costs would you weigh?Backward compatibility
Which of these changes to a JSON response are backward compatible for existing clients: adding a field, renaming a field, changing a number to a string, adding a new enum value, making an optional request field required?You need to split the field name intofirstName and lastName in a widely used customer API without a new major version. Describe the steps and how long each phase lasts.How do you design for backward compatibility across many services that deploy independently, so that rolling deploys and mixed versions never break calls between them?What organisational practices determine whether an engineering organisation can keep APIs compatible for many years, beyond the technical techniques?REST vs SOAP
Compare REST over HTTP with JSON and SOAP web services: contract, message format, error handling, tooling and security standards. When would you still choose SOAP today?A Spring Boot service must call a partner's SOAP API that uses WS-Security signatures, while exposing a REST API to your own clients. How would you structure the integration and test it?Your company still runs 70 SOAP services built 15 years ago, used by partners and internal systems. Should you rewrite them as REST? How do you decide and plan over several years?Long-running operations
A POST /reports request takes 5 to 20 minutes to generate a report. Design the API so clients do not hold an HTTP connection that long.Implement the long-running job pattern in Spring Boot: POST starts the work asynchronously and returns 202, GET returns status, and jobs survive a pod restart. What components do you use?Define a platform standard for asynchronous and long-running operations across 50 teams' REST APIs, covering status resources, callbacks and failure semantics.OpenAPI code generation
Configure the OpenAPI Generator Maven plugin to generate Spring server interfaces from anorders.yaml spec, and explain how you implement the generated interface without editing generated code.Clients generated from your OpenAPI spec in Java, TypeScript and Kotlin keep breaking: enum additions crash old clients, nullable fields are typed wrongly and model names change between generator versions. How do you make code generation reliable?Should your organisation generate server stubs from OpenAPI (design-first) for all teams, or let teams generate the spec from code? Lay out how you would decide and roll it out.No question matches these choices.