Quick revision
The short answer
I would make API-first the long-term default for public and cross-team APIs: the OpenAPI spec is written and reviewed before code, linted in CI, and used to generate server interfaces and clients. Code-first, generating the spec from annotations, stays acceptable for internal APIs owned by one team, as long as the spec is still published and checked for breaking changes. The key point is to pick one default for most cases, so tooling and skills do not split across both approaches.
What to remember
In this answer, we saw that code-first is faster at the start but lets the contract drift, while API-first makes the contract reviewable before code, supports generated clients and catches drift at compile time. I would choose API-first with linting as the default for public and cross-team APIs, allow code-first for internal APIs owned by one team, require every spec to be published, linted and checked for breaking changes in CI either way, and keep one default to limit tool diversity.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would make API-first the long-term default for public and cross-team APIs: the OpenAPI spec is written and reviewed before code, linted in CI, and used to generate server interfaces and clients. Code-first, generating the spec from annotations, stays acceptable for internal APIs owned by one team, as long as the spec is still published and checked for breaking changes. The key point is to pick one default for most cases, so tooling and skills do not split across both approaches.
Why this question is asked
In a company with dozens of teams, both approaches usually exist side by side, and both have defenders. Code-first teams say they ship faster; API-first teams say their contracts are better. At a senior level, the interviewer wants to see a reasoned standard, not a preference, plus a plan that works for both kinds of team. In this answer, we will recap the two approaches with a small example, compare what each costs over the life of an API, choose a default, and define the rules that apply whatever the choice.
1A Quick Recap of the Two Approaches
In code-first design, we write the Spring controller and its DTOs, annotate them, and a tool such as springdoc-openapi generates the OpenAPI document from the running code or during the build. The code is the source of truth, and the spec is a by-product.
In API-first design, we write the OpenAPI document first, as YAML, and review it with the people who will call the API. Then a generator such as openapi-generator produces a Java interface and model classes, and the controller implements that interface. Clients in other languages are generated from the same file. The spec is the source of truth, and the code must conform to it.
2What Each Approach Costs Over Time
Code-first is fast at the start. The developer writes Java, which they know well, and the spec appears for free. For an internal API with one consumer that sits in the next room, that is often enough. The cost shows up later. Because the spec is generated, nobody reviews it as a design artifact, and it drifts with every refactoring: a renamed DTO field silently renames a JSON property, a changed annotation changes a status code. Consumers discover the change when their integration breaks. The generated document also tends to be less precise, with missing descriptions, examples and error responses.
API-first costs more at the start. Someone must write YAML, which feels slower, and the team needs a generator setup and some discipline when the spec and code meet. But it makes the contract reviewable before code exists, so consumers can comment and even build against mocks in parallel. It supports generated clients, so consumers in TypeScript, Python or Kotlin get typed code for free. And the build fails if the implementation stops matching the interface, so drift is caught at compile time.
| Code-first | API-first | |
|---|---|---|
| Source of truth | Java code and annotations | The OpenAPI document |
| Speed at the start | Fast | Slower: write and review the spec |
| Review before coding | Hard | Natural; consumers can comment |
| Contract drift | Easy to drift unnoticed | Implementation must match the spec |
| Generated clients | Possible, from a by-product | First-class, from the reviewed file |
| Best fit | Internal API, one owning team | Public and cross-team APIs |
3Choosing the Default
The deciding factor is who consumes the API and how hard it is to change. A public API used by partners, or an API called by several other teams, is expensive to change once released, and its consumers are not present when the code is written. For those, reviewing the contract first and generating clients from it pays back many times. I would make API-first, with a shared lint ruleset, the default for them.
For internal APIs that one team owns and consumes, such as a back end for its own front end, code-first is acceptable. Forcing YAML-first there adds ceremony without much benefit. But it must still meet the same publication and compatibility rules, below.
What I would avoid is leaving it to every team with no default. Two approaches mean two sets of generator configuration, two ways of documenting, and engineers who move between teams relearning the workflow. One default for the common case, with a documented exception, keeps the tooling and the skills concentrated.
4Rules That Apply Whatever the Choice
Whichever way a spec is produced, every service publishes it to a central catalogue on every release, so consumers can always find the current contract. Every spec is linted with the same Spectral ruleset, so naming, errors and paging look the same. And every pipeline compares the new spec with the last released one using a tool such as oasdiff, failing on breaking changes unless a new version is declared.
- Who consumes it?Partners, the public or other teams?YesAPI-first: spec reviewed firstNoCode-first allowed
- Generate server interfaces and clientsopenapi-generator,
interfaceOnly - Publish, lint and diff in CISame steps for both approaches
- Breaking change detected?oasdiff against last releaseYesNew version or explicit approvalNoRelease
4 steps in order. Play them, or step through them one at a time.
Notice that this pipeline gives code-first teams most of the safety of API-first: drift is caught, though only after the code is written rather than before. That is the compromise that lets both approaches coexist under one standard.
5Follow-up questions
How do you move code-first teams to API-first without stopping them?
Start with their existing generated spec as the first version of the hand-maintained file, so nothing is rewritten. From then on, changes start in the spec, and the generator produces an interface that the existing controllers implement. Do it for public APIs first, where the benefit is largest, and help with the generator setup through a shared build plugin.
Do generated server stubs make code harder to work with?
They can, if the generator produces whole controllers. Generating only interfaces and models, with interfaceOnly set, keeps the controller hand-written and readable while the compiler enforces the contract. Some teams generate only models and keep controllers fully manual, checking conformance with contract tests instead.
6Conclusion
In this answer, we saw that code-first is faster at the start but lets the contract drift, while API-first makes the contract reviewable before code, supports generated clients and catches drift at compile time. I would choose API-first with linting as the default for public and cross-team APIs, allow code-first for internal APIs owned by one team, require every spec to be published, linted and checked for breaking changes in CI either way, and keep one default to limit tool diversity.
Open with
I'd choose API-first as the long-term standard for public and cross-team APIs, and allow code-first for internal APIs that one team owns and consumes. The reasoning is about who pays when the contract changes.
Then, if they want more
With code-first, the spec is a by-product of the Java code, so it drifts with every refactoring and nobody reviews it as a design. That's fine when the only consumer is the same team, but painful for partners.
With API-first, the OpenAPI file is written and reviewed before code, so consumers can comment and even build against mocks in parallel, we generate typed clients for other languages, and generating server interfaces with interfaceOnly means the compiler catches drift. It's slower at the start, but public APIs live for years.
Whatever approach a team uses, the same rules apply: publish the spec to a central catalogue on every release, lint it with a shared Spectral ruleset, and run a breaking-change check like oasdiff in CI against the last release. That gives code-first teams most of the safety too.
And I'd insist on one default rather than leaving it open, because two approaches double the generator setups, documentation styles and things engineers need to learn when they move between teams. Migrating is gradual: take a team's generated spec as the starting file and make future changes spec-first.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "API-first design with OpenAPI and code-first design with annotations both exist in your...": https://scrivio-interview-library.netlify.app/spring-boot/rest-controllers/683ba7cc57/