Quick revision
The short answer
We add org.openapitools:openapi-generator-maven-plugin with generatorName spring, inputSpec pointing at orders.yaml, and configOptions such as interfaceOnly=true and useSpringBoot3=true (option names vary by generator version, so check yours). The plugin writes the OrdersApi interface and the models to target/generated-sources, which we never commit or edit. We write our own @RestController that implements OrdersApi, map the generated DTOs to our domain objects, and let the build regenerate the code so a spec change breaks compilation exactly where our code must change.
What to remember
In this answer, we configured openapi-generator-maven-plugin with generatorName spring, inputSpec pointing at orders.yaml, and options such as interfaceOnly, useSpringBoot3 and skipDefaultInterface. The generated OrdersApi and DTOs live in target/generated-sources and are never committed or edited. Our own @RestController implements OrdersApi and returns ResponseEntity types, a mapper keeps the domain independent of the generated DTOs, and because generation runs in every build, a spec change breaks compilation exactly where our code must change.
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.
We add org.openapitools:openapi-generator-maven-plugin with generatorName spring, inputSpec pointing at orders.yaml, and configOptions such as interfaceOnly=true and useSpringBoot3=true (option names vary by generator version, so check yours). The plugin writes the OrdersApi interface and the models to target/generated-sources, which we never commit or edit. We write our own @RestController that implements OrdersApi, map the generated DTOs to our domain objects, and let the build regenerate the code so a spec change breaks compilation exactly where our code must change.
Why this question is asked
Design-first APIs start from an OpenAPI file that the team reviews before writing code. Generating the server side from that file keeps the code and the contract identical, but only if we set the generator up so that regenerating never overwrites our work. Interviewers ask this to see whether we know the practical setup, where generated code goes and how we keep our own code separate from it. In this answer, we will look at a small spec, configure the plugin, see what it generates, implement the interface in our own controller, keep the domain independent with a mapper, and see how a spec change shows up in the build.
1A Small orders.yaml to Start From
Here is the part of the spec we will generate from: one operation to fetch an order. The operationId becomes the Java method name and the tag decides which interface it lands in.
Notice that the schema has an explicit name, Order. Named schemas under components give stable class names; inline schemas get generated names that can change, which is a common source of trouble.
2Configuring the Maven Plugin
The plugin runs in the generate-sources phase, before compilation, and adds its output folder to the compile path automatically.
Each option has a reason. interfaceOnly=true stops the generator from writing a controller class we would be tempted to edit. useSpringBoot3=true makes it use jakarta packages and Spring 6 conventions; newer generator versions may offer a different switch for Spring Boot 4, so check your version. skipDefaultInterface=true matters more than it looks: without it, every method gets a default body that returns 501 Not Implemented, so a newly added operation would compile and quietly answer 501 in production.
3What Gets Generated and Where
After mvn generate-sources, the code is in target/generated-sources/openapi. It is a build output, like compiled classes: it is not committed to Git and nobody edits it, because the next build overwrites it.
Notice that the mapping annotations sit on the interface method. Spring MVC detects request mappings and parameter annotations declared on an interface that a controller implements, so our class does not repeat them.
4Implementing the Interface in Our Own Controller
Our code lives in src/main/java as usual. The controller implements OrdersApi and returns the ResponseEntity types the interface declares.
So regeneration never touches this file, and the compiler checks that our method signature matches the contract. If we need to customise something the generator produces, we change the spec or the generator options, never the generated file.
5Keeping the Domain Independent of the Spec
It is tempting to pass OrderDto straight into the service layer and the database. That ties our business code to the API contract: every rename in the spec ripples through the domain, and the domain can no longer change without changing the public API. So we map at the edge. The service works with our own Order record, and a small mapper converts between the two, by hand or with MapStruct.
The mapper is the one place where the spec and the domain meet. When the API evolves, for example a new field or a renamed enum value, the change is made in the spec and in the mapper, and the domain stays as it was.
6Letting a Spec Change Break the Build
Because generation runs in every build, the spec is not documentation that can drift; it is source code. Suppose a reviewer approves adding a required query parameter, includeItems, to getOrder. The next build regenerates OrdersApi with a new method signature, and our controller no longer compiles.
- Edit and review orders.yamlthe contract is the source
- mvn generate-sources
OrdersApiand DTOs in target/generated-sources - Does OrdersController still compile?YesTests run, service shipsNoFix the controller or mapper, not generated code
- Spec published with the buildclients generated from the same file
4 steps in order. Play them, or step through them one at a time.
That error is the feature. It points to exactly the place where our code must change to match the contract, before anything reaches a test environment. The same goes for a new operation: with skipDefaultInterface=true, it appears as an abstract method we must implement. Pinning the generator version makes sure these changes come only from the spec, not from a generator upgrade.
7Follow-up questions
Should the generated code be committed to Git?
No. It is a reproducible build output, so committing it only creates large diffs and the temptation to edit it. If we want to see what changed after a generator upgrade, we generate into a temporary folder in CI and diff it there.
How do you add custom annotations to generated models?
Through the spec or the generator, not by editing files. The spring generator supports vendor extensions such as x-field-extra-annotation for some cases, and we can also provide custom Mustache templates. Both keep the customisation reproducible; check your generator version for the supported extensions.
8Conclusion
In this answer, we configured openapi-generator-maven-plugin with generatorName spring, inputSpec pointing at orders.yaml, and options such as interfaceOnly, useSpringBoot3 and skipDefaultInterface. The generated OrdersApi and DTOs live in target/generated-sources and are never committed or edited. Our own @RestController implements OrdersApi and returns ResponseEntity types, a mapper keeps the domain independent of the generated DTOs, and because generation runs in every build, a spec change breaks compilation exactly where our code must change.
Open with
I add the openapi-generator-maven-plugin from org.openapitools with the generate goal, generatorName spring, and inputSpec pointing at src/main/resources/openapi/orders.yaml. I set apiPackage and modelPackage, a model name suffix like Dto, and configOptions: interfaceOnly true so it generates only the interface and no controller class, useSpringBoot3 true for jakarta and Spring 6 conventions, although the option name for Boot 4 depends on the generator version, useTags so I get one OrdersApi per tag, and skipDefaultInterface true, so new operations are abstract instead of quietly returning 501.
Then, if they want more
The plugin writes everything to target/generated-sources/openapi and adds it to the compile path; it is not committed and nobody edits it. Then I write my own OrdersController with @RestController that implements OrdersApi. The mapping annotations are on the interface, so my class just overrides getOrder and returns ResponseEntity.ok or notFound.
I do not pass the generated DTOs into the service layer; a mapper converts OrderDto to my own domain Order, so the domain does not depend on the spec.
And because generation runs in every build with a pinned generator version, when the spec changes, for example a new required parameter, the controller stops compiling right where I need to change it, which is exactly the feedback I want.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 4
- 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 "Configure the OpenAPI Generator Maven plugin to generate Spring server interfaces from...": https://scrivio-interview-library.netlify.app/spring-boot/rest-api-design/da83a7e3cb/