Scrivio Interview library
DesignIntermediate7 min read · updated 12 November 2026

A partner publishes an XSD that changes twice a year. Would you generate classes with xjc (contract first) or annotate Java classes and generate the XSD (code first)? How would you build it?

Because the partner owns the XSD, I would go contract first: keep a versioned copy of each schema release in the repository and generate the classes with xjc in the build, through the JAXB Maven plugin or a Gradle equivalent, with binding files (.xjb) for package names, type mappings and adapters. The generated classes live in their own module and are mapped to domain objects, so a schema change twice a year touches only that module and its mapper. Code first with schemagen suits contracts we own, but it gives less control over the schema. Each change gets a new version and is tested against the partner's sample documents.

Why this question is asked

With XML integrations, there are two ways to keep Java and schema in step. In contract first, the XSD is the source of truth and Java classes are generated from it. In code first, the Java classes are the source and the XSD is generated from them. Interviewers ask which one we would choose when a partner's schema changes regularly, because the answer depends on who owns the contract, and the build design that follows shows whether we have kept such an integration alive across many versions. In this answer, we will compare the two approaches, choose one for this case, build the code generation, isolate the generated code, customise it with binding files, and handle each schema change safely.

1A Quick Recap of the Two Approaches

xjc is the JAXB schema compiler: it reads an XSD and generates annotated Java classes, plus an ObjectFactory and a package-info.java, that marshal to and from documents valid against that schema. schemagen does the opposite: it reads annotated Java classes and writes an XSD describing them. Contract first uses xjc; code first uses schemagen, or simply lets JAXB produce whatever XML the annotations describe.

Contract first versus code first
QuestionContract first (xjc)Code first (schemagen)
Source of truththe XSDthe Java classes
Who owns the contractthe partner, or a standards bodyour team
Control over the schemaexact, written by its ownerlimited, generated from annotations
Effect of a partner changereplace the XSD, regenerateedit classes by hand to match
Java classesgenerated, never editedhand-written, can be idiomatic

2Choosing for a Partner-Owned Schema

Here the partner publishes the XSD and changes it twice a year. The XSD is therefore the contract, and our job is to conform to it exactly. With contract first, adopting a new release means dropping the new XSD into the repository and rebuilding: every new element, changed type and new namespace appears in the generated classes, and the compiler shows us which mapping code must change.

With code first, we would have to read the partner's changes and reproduce them by hand in annotations, then hope that the schema JAXB generates from our classes matches theirs. Generated schemas also tend to differ in details the partner cares about, such as named versus anonymous types, element form and facets like patterns and lengths, which annotations cannot fully express. Code first is fine when we own the contract and the consumers adapt to us; it is the wrong tool when someone else's schema is the law.

3Generating Classes in the Build

The schemas live in the repository as versioned copies, exactly as published, for example src/main/xsd/partner-orders-v12.xsd. We never download schemas during the build, because a build must be reproducible and must not change because the partner edited a file on their website. A JAXB code generation plugin runs xjc during the generate-sources phase, so the classes are always in step with the schema and are never committed or edited by hand. Plugin coordinates and versions differ between the Jakarta-compatible plugins, so check your plugin's documentation.

XML
<!-- integration-partner-orders/pom.xml -->
<plugin>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-maven-plugin</artifactId> <!-- a Jakarta-compatible 4.x line; check your version -->
<executions>
<execution>
<goals><goal>generate</goal></goals>
<configuration>
<schemaDirectory>src/main/xsd</schemaDirectory>
<schemaIncludes><include>partner-orders-v12.xsd</include></schemaIncludes>
<bindingDirectory>src/main/xjb</bindingDirectory>
<strict>true</strict>
</configuration>
</execution>
</executions>
</plugin>
// mvn generate-sources
// -> target/generated-sources/xjc/com/acme/partner/orders/v12/OrderType.java, ObjectFactory.java, ...

Gradle builds do the same with a community xjc plugin or a small task that runs the XJC tool from the jaxb-xjc artifact. What matters is the principle: generation is part of every build, from a pinned schema version, with no manual step.

4Isolating Generated Code in Its Own Module

The generated classes go into a dedicated module, for example integration-partner-orders, which contains the schemas, the binding files and nothing else except tests. The rest of the service never imports these classes directly. Instead, a mapper in the integration layer converts between generated types and our domain objects.

The reason is change containment. Generated classes follow the partner's naming and structure, use JAXBElement and XMLGregorianCalendar in places, and change whenever the schema does. If our services used them as domain objects, every schema release would ripple through business logic, persistence and APIs. With a mapper in between, a schema change breaks only the mapper, and the compiler lists exactly which lines to fix.

Step throughModule structure for one partner
  1. Partner XSD v12 (copied, versioned)src/main/xsd, changed only by a new release
  2. Binding files (.xjb)package names, adapters, type mappings
  3. Generated classesrebuilt every build, never edited or committed
  4. Mapperthe only code that imports generated types
  5. Domain model and servicesunaware of the partner's schema

5 layers, from the top down. Step through them one at a time.

A schema release changes the top three layers and the mapper, never the domain.

5Customising With Binding Files

xjc's defaults are not always what we want: package names derived from namespace URIs, XMLGregorianCalendar for dates, awkward class names. Binding files, external XML files with the .xjb extension, customise the generation without touching the partner's XSD, which must stay exactly as published.

XML
<!-- src/main/xjb/partner-orders.xjb -->
<jaxb:bindings version="3.0"
xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
jaxb:extensionBindingPrefixes="xjc">
<jaxb:globalBindings>
<!-- xs:date becomes java.time.LocalDate through our adapter -->
<xjc:javaType name="java.time.LocalDate" xmlType="xs:date"
adapter="com.acme.xml.adapters.LocalDateAdapter"/>
</jaxb:globalBindings>
<jaxb:bindings schemaLocation="../xsd/partner-orders-v12.xsd" node="/xs:schema">
<jaxb:schemaBindings>
<jaxb:package name="com.acme.partner.orders.v12"/> <!-- version in the package name -->
</jaxb:schemaBindings>
</jaxb:bindings>
</jaxb:bindings>

Check your plugin version for the exact binding namespace and version attribute; Jakarta-era tools use the jakarta.ee namespace shown here, while older javax tools used java.sun.com. Putting the schema version into the package name is optional, but it allows two versions to exist side by side during a partner's transition period, when they accept both old and new messages for a while.

6Handling Each Schema Change

Each partner release follows the same routine. We add the new XSD as a new file, such as v13, rather than overwriting v12, so the change is visible in review as a diff between two files. We regenerate, fix the mapper where the compiler complains, and decide how to treat new optional elements. Then we run validation tests: the partner's own sample documents for the new version must unmarshal with the schema set on the unmarshaller, and documents we produce must validate against the new XSD.

Twice a year is frequent enough that this routine should be written down and owned by a named person, with the partner's release calendar in the team's planning. A release that arrives without warning is much easier to absorb when regeneration and the test suite already exist and take an afternoon instead of a sprint.

7Follow-up questions

When would you choose code first?

When we own the contract and its consumers follow us, for example an internal XML API or a new contract we publish to partners. Even then, many teams write the XSD by hand once and switch to contract first, because a generated schema is hard to keep stable and readable across releases.

Should generated sources be committed?

Normally no, because they are a pure function of the schema and binding files, and committed copies invite hand edits. The exception is a slow or unreliable generator, in which case the build should still regenerate and fail if the committed files differ.

How do you support two schema versions at once?

Generate both into different packages, such as v12 and v13, in the same or separate modules, with one mapper per version to the same domain model. Route incoming messages by namespace or version attribute, and remove the old version when the partner retires it.

8Conclusion

In this answer, we saw that the owner of the contract decides the approach. Since the partner owns the XSD, we chose contract first: versioned copies of each schema in the repository, xjc in the build through a Jakarta-compatible plugin, binding files for packages, types and adapters, and generated classes isolated in their own module behind a mapper. Code first with schemagen fits contracts we own. Every schema change becomes a new version, regenerated and tested against the partner's sample documents and the XSD itself.

What to say in the interviewAbout 90 seconds aloud

Open with

Since the partner owns the XSD, I would go contract first. The schema is the law, and generating classes from it means a new release shows up automatically in the code, while the compiler tells me what to fix.

Then, if they want more

Code first with schemagen is fine when I own the contract, but a generated schema is hard to control and rarely matches a partner's exactly.

In the build, I would keep a versioned copy of each published XSD in the repository, never download it during the build, and run xjc through a Jakarta-compatible JAXB Maven plugin or a Gradle equivalent in the generate-sources phase, so generated classes are never edited or committed.

I would put them in their own integration module with a mapper to our domain objects, so a schema change ripples only to the mapper. Binding files customise generation without touching the partner's XSD: the package name, with the version in it if two versions must coexist, and type mappings such as xs:date to LocalDate through an adapter.

For each release, I add the new XSD as a new version file so the diff is reviewable, regenerate, fix the mapper, and run validation tests with the partner's sample documents and our own output against the new schema. With two releases a year, I would make that routine written and owned.

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 official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
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 "A partner publishes an XSD that changes twice a year. Would you generate classes with...": https://scrivio-interview-library.netlify.app/java/jaxb/a8bc1411a1/