Quick revision
The short answer
I would design a small public surface, a single PaymentsClient and a few request, result and error types, with all implementation classes hidden in internal packages that the module does not export. The API exposes only our own types, never those of the HTTP client or JSON library underneath, so we can swap dependencies later. Configuration is an immutable object built with a builder and safe defaults such as timeouts, errors are explicit through a small exception hierarchy or result types, thread safety and lifecycle are documented, and releases follow semantic versioning with binary compatibility checked in CI by a tool such as japicmp or revapi.
What to remember
In this answer, we designed a payments client library around the fact that every public element is a long-term promise to 100 services. We kept the public surface to one client and a few types and hid everything else in internal, unexported packages, exposed only our own types so dependencies can change, used an immutable builder-based configuration with safe timeouts and retries, made errors explicit with result types and a small exception hierarchy, documented thread safety and lifecycle, and protected consumers with semantic versioning and binary compatibility checks in CI.
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 design a small public surface, a single PaymentsClient and a few request, result and error types, with all implementation classes hidden in internal packages that the module does not export. The API exposes only our own types, never those of the HTTP client or JSON library underneath, so we can swap dependencies later. Configuration is an immutable object built with a builder and safe defaults such as timeouts, errors are explicit through a small exception hierarchy or result types, thread safety and lifecycle are documented, and releases follow semantic versioning with binary compatibility checked in CI by a tool such as japicmp or revapi.
Why this question is asked
A library used by 100 services is a different kind of code from an application. Every public class and method becomes a promise that 100 teams depend on, and every mistake in the API is copied 100 times before we can fix it. A breaking change means coordinating dozens of upgrades. Interviewers ask this design question to see whether we think about the consumers' experience and the long-term cost of each public decision, not just about making the HTTP call work. In this answer, we will keep the public surface small, hide our dependencies, design the configuration, make errors explicit, document threading and lifecycle, and finish with how versioning and compatibility checks keep 100 services safe over years.
1Keep the Public Surface Small
The most important principle is that everything public is permanent. Once 100 services call a method, removing or changing it is expensive, so we expose as little as possible. In practice the public API is a single entry point, PaymentsClient, plus the request, response and error types it uses. Everything else, the HTTP calls, the retry logic, the JSON mapping and the signing of requests, lives in internal packages.
Java gives us two tools to enforce this. Within a package, classes can be package-private. Across packages, the Java module system lets us export only the API package, so internal classes cannot be used even if they are public. Many Spring Boot services do not run on the module path, so we also name the package internal and add an ArchUnit rule or a documented convention that it must not be used.
- Public API
PaymentsClient,ChargeRequest,ChargeResult,PaymentExceptiontypes - Configuration
PaymentsClientConfigwith builder and safe defaults - Internal: behaviourretries, idempotency keys, request signing, metrics
- Internal: transportHTTP client and JSON mapping, replaceable at any time
- Optional Spring Boot starterauto-configuration that builds one client from properties
5 layers, from the top down. Step through them one at a time.
Only the top two layers are visible to the 100 consuming services.
2Expose Our Own Types, Not Our Dependencies' Types
If our API returns an OkHttp Response, takes a Jackson JsonNode or throws an Apache HttpClient exception, those libraries become part of our contract. Consumers start depending on them, and we can never switch HTTP client or JSON library without breaking 100 services. Worse, consumers inherit version conflicts with whatever versions they use themselves.
Notice that ChargeRequest also uses a Money type instead of a bare BigDecimal and a currency string, so consumers cannot send an amount without a currency. The internal layer maps these types to whatever wire format the payments platform uses today.
3Immutable Configuration With Safe Defaults
Most production incidents with client libraries come from configuration: no timeouts, so a slow payments platform hangs every caller's threads, or retries on non-idempotent calls, so a customer is charged twice. The library should make the safe choice the default and make dangerous choices explicit.
The config is immutable, so it can be shared safely between threads and cannot be changed after the client is built. The builder validates required values in build() and fails fast with a clear message. Retries attach an idempotency key automatically, so a retried charge is processed once on the platform side. Defaults are documented, so teams know what they get without reading the source.
4Make Errors Explicit
Callers need to make different decisions for different failures: a declined card is a normal business outcome to show to the customer, while a timeout may be safe to retry later, and an invalid request is a bug. A single generic exception forces callers to parse messages. Two designs work well. One is a small, unchecked exception hierarchy. The other is to return expected business outcomes as result types and throw only for technical failures.
A decline is a normal answer, so it is a value the compiler forces callers to handle. Technical failures are exceptions with a clear type and a retryable flag, so callers can decide without string matching. The hierarchy stays small, because every exception type is also public API.
5Document Thread Safety and Lifecycle
Consumers need to know two things that the type system does not tell them. Is the client safe to share between threads, and does it hold resources that must be released? For a payments client the right answer is usually: create one instance per application, share it across all threads, and close it on shutdown, because it holds a connection pool. We say this in the Javadoc of PaymentsClient, make it implement AutoCloseable, and provide a Spring Boot starter that creates a single bean from properties, so Spring closes it automatically.
Without this documentation, some team will create a new client per request, opening a new connection pool each time, and another will close a shared client in one component while others still use it.
6Versioning and Binary Compatibility in CI
With 100 consumers, version numbers are a communication tool. We follow semantic versioning: a patch release fixes bugs, a minor release adds features compatibly, and only a major release may break the API. But humans are bad at spotting breaking changes, especially binary ones, such as changing a return type or adding a method to an interface that consumers implement, which compile in our project but fail with NoSuchMethodError or AbstractMethodError in theirs.
So the build compares every release candidate against the last released version with a tool such as japicmp or revapi and fails if a minor or patch release contains an incompatible change. We publish the library through the organisation's BOM so services get a tested combination of versions, and we keep a changelog that explains every change in terms of what consumers must do.
7Follow-up questions
Why not just generate the client from the payments OpenAPI specification?
Generated clients are a good internal layer, but they usually expose every endpoint and the generator's own types, so the public surface changes whenever the specification or the generator does. Wrapping a generated client behind a small hand-designed API gives us the speed of generation with a stable contract.
Should the library offer a reactive or asynchronous API?
Only if consumers need it. Each style doubles the surface we must keep compatible. With virtual threads in Java 21 and later, a simple blocking API scales well for most Spring MVC services. If several teams use WebFlux, an additional asynchronous method returning CompletableFuture is easier to support than a full reactive API.
How do you get feedback before the API is frozen?
Release the first version to two or three pilot services, mark new APIs as experimental, for example with an annotation and a note in the documentation, and change them freely until the pilots are happy. Only then promise compatibility with a 1.0 release.
8Conclusion
In this answer, we designed a payments client library around the fact that every public element is a long-term promise to 100 services. We kept the public surface to one client and a few types and hid everything else in internal, unexported packages, exposed only our own types so dependencies can change, used an immutable builder-based configuration with safe timeouts and retries, made errors explicit with result types and a small exception hierarchy, documented thread safety and lifecycle, and protected consumers with semantic versioning and binary compatibility checks in CI.
Open with
The principle I start from is that everything public is a promise to 100 teams, so I keep the surface small. Consumers see one PaymentsClient and a few request, result and exception types.
Then, if they want more
Retries, signing, JSON mapping and the HTTP transport live in an internal package that the module doesn't export, and I'd add an ArchUnit rule for services not on the module path. I never expose types from dependencies, like an HTTP client response or a Jackson node, because then we could never change those libraries and consumers would inherit version conflicts.
Configuration is an immutable object built with a builder that validates on build and has safe defaults: a two second connect timeout, a five second request timeout, and limited retries only with an idempotency key, so retries can't double charge. Errors are explicit.
Expected business outcomes like a decline come back as a sealed ChargeResult that the compiler makes callers handle, and technical failures are a small unchecked hierarchy with a retryable flag. I'd document that the client is thread safe, should be created once and closed on shutdown, make it AutoCloseable, and ship a Spring Boot starter that creates a single bean.
Finally, I follow semantic versioning and run japicmp or revapi in CI against the last release, so a minor release can't break binary compatibility, and publish it through the shared BOM with a clear changelog.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21
- 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 "Design the public API of an internal Java client library for the payments platform that...": https://scrivio-interview-library.netlify.app/java/clean-code/df1a8e8b17/