Scrivio Interview library
DesignIntermediate6 min read · updated 12 November 2026

An API exposes /customers/{id}/accounts/{accountId}/transactions with many nested operations, all in one 2,000-line resource class. How would you restructure it using sub-resource locators, and what are the trade-offs versus separate root resources?

I would split the class with sub-resource locators: methods that have @Path but no HTTP method annotation and return an object whose methods handle the rest of the path, such as CustomerResource returning an AccountResource that returns a TransactionsResource. The parent loads and authorizes the customer once and passes it down, and the sub-resources are created through ResourceContext so they still get injection. The cost is that locators are resolved at runtime, so OpenAPI and WADL output can be less accurate, and deep nesting ties URLs to the object graph, which is why flatter root resources such as /accounts/{accountId}/transactions with explicit ownership checks are often the better long-term shape.

Why this question is asked

A 2,000-line resource class is hard to read, hard to test and a magnet for merge conflicts, and nested URLs make it tempting to keep everything together. Interviewers ask this design question to see whether we know the JAX-RS feature built for nested resources, how it interacts with injection and authorization, and when not to use it. In this answer, we will recap what a sub-resource locator is, restructure the class step by step, see how injection works for sub-resources, use the parent to centralise authorization, and weigh locators against flatter root resources.

1A Quick Recap of Sub-resource Locators

In Jakarta REST, a resource method has an HTTP method annotation such as @GET and handles a request. A sub-resource locator is different: it has @Path but no HTTP method annotation, and instead of handling the request it returns an object, or a Class that the runtime instantiates. The runtime then continues matching the remaining part of the URL against that object's methods. The returned class has no @Path of its own, because its position in the URL is decided by the locator.

So a request to /customers/7/accounts/3/transactions can pass through three objects: the customer resource matches customers/{customerId}, a locator for accounts/{accountId} returns an account resource, and its locator for transactions returns a transactions resource whose @GET finally runs.

Step throughGET /customers/7/accounts/3/transactions
RuntimeCustomerResourceAccountResourceTransactionsResource
match customers/{customerId}
call locator account(7, 3)
load customer 7, check the caller owns it
AccountResource for account 3
call locator transactions()
TransactionsResource
invoke @GET list()
200 with transactions

8 messages in order. Play them, or step through them one at a time.

2Splitting the Class With Locators

We keep the root class small. It owns only the customer-level operations and the locator for accounts, which performs the checks every nested request needs.

Java
@Path("customers/{customerId}")
public class CustomerResource {
@Inject CustomerService customers;
@Context ResourceContext resources;
@Context SecurityContext security;
@GET
public CustomerDto get(@PathParam("customerId") long id) { return customers.view(id); }
@Path("accounts/{accountId}") // no @GET: this is a locator
public AccountResource account(@PathParam("customerId") long customerId,
@PathParam("accountId") long accountId) {
Account account = customers.ownedAccount(customerId, accountId, security.getUserPrincipal())
.orElseThrow(NotFoundException::new); // 404 if missing or not the caller's
return resources.initResource(new AccountResource(account));
}
}
public class AccountResource { // no @Path on the class
@Inject TransactionService transactions;
@Context ResourceContext resources;
private final Account account;
public AccountResource(Account account) { this.account = account; }
@GET
public AccountDto get() { return AccountDto.of(account); }
@Path("transactions")
public TransactionsResource transactions() {
return resources.initResource(new TransactionsResource(account));
}
}

Each nested class now has one responsibility and a few hundred lines at most. The transactions operations, such as listing, getting one and reversing one, live in TransactionsResource, and each can be tested with a known Account without going through the parent.

3Why the Sub-resource Must Go Through ResourceContext

A tempting shortcut is to return new AccountResource(account). That compiles and the request reaches the sub-resource, but the object was created by our code, not by the runtime, so its @Context fields and CDI @Inject fields stay null. The first call to transactions throws a NullPointerException.

ResourceContext solves this. resources.getResource(AccountResource.class) asks the runtime to create an instance with full injection, and resources.initResource(instance) injects into an object we created ourselves, which is useful when we want to pass state through the constructor as above. The reason the distinction exists is that the runtime can only inject objects it knows about.

4Centralising Authorization in the Parent

The biggest benefit of locators is not shorter files. It is that every request under /customers/{customerId}/accounts/{accountId} must pass through the account locator, so the ownership check is written once. In the 2,000-line class, each of perhaps 40 methods loaded the customer and checked ownership itself, and it only takes one forgotten check to expose another customer's transactions.

Notice that the locator returns 404, not 403, when the account belongs to someone else. That avoids telling an attacker that account 3 exists. The child resources receive an Account that has already been checked, so they cannot accidentally act on an unchecked id.

5Trade-offs Compared With Separate Root Resources

Locators have costs. They are resolved at runtime, so tools that generate OpenAPI or WADL documents must infer the sub-resource from the return type. That works when the locator returns a concrete class, but returning Object or choosing the class dynamically leaves gaps in the documentation, and the exact behaviour depends on the generator. Deep nesting also couples the URL to the object graph: if accounts later belong to households instead of customers, every URL under the customer changes.

The alternative is flatter root resources, such as @Path("accounts/{accountId}/transactions") on its own class, where each request checks that the caller may access the account through a shared authorization service. Those are easier to document, to version and to move to another service later. A common compromise is to keep one level of nesting where the parent truly scopes the child and use flat roots elsewhere.

Locators compared with flat root resources
AspectSub-resource locatorsFlat root resources
AuthorizationOnce in the parent locatorRepeated per resource, via a shared service
DocumentationGenerated from return types, may be partialStatic, accurate OpenAPI
URL stabilityTied to the object graphIndependent of ownership changes
TestingChild testable with a given parent objectEach resource tested on its own
Splitting into servicesHarder, nested paths move togetherEasier, one root per service

6Follow-up questions

How do you keep authorization consistent across nested resources?

Put the rule in one place that every path must pass. With locators, that is the parent locator, which loads the object scoped to the caller and passes only checked objects down. With flat resources, it is a shared authorization service or a name-bound ContainerRequestFilter that checks ownership of the id in the path, combined with queries that always filter by owner. In both cases we add tests that call every endpoint as a different user and expect 404.

Can a locator return different classes for different requests?

Yes. A locator can return any object, for example a VersionedAccountResource for one API version and another class for the next. This is flexible, but it makes static documentation harder, so it should be used sparingly.

Do sub-resources share the request's lifecycle?

Yes. Objects returned by locators live only for the current request, like per-request root resources, so passing request data such as the loaded Account through their constructor is safe.

7Conclusion

In this answer, we saw that sub-resource locators, methods with @Path and no HTTP method that return the next resource object, let us split a huge nested resource into small classes. Creating sub-resources through ResourceContext keeps injection working, and the parent locator becomes the single place that loads and authorizes the customer. The trade-offs are less precise generated documentation and URLs tied to the object graph, so flatter root resources with explicit ownership checks are often the better choice for deeper levels.

What to say in the interviewAbout 90 seconds aloud

Open with

I would break the class up along the URL with sub-resource locators. A locator is a method with @Path but no HTTP method annotation; the runtime calls it during matching, and it returns an object whose methods handle the rest of the path.

Then, if they want more

So CustomerResource keeps the customer operations and has a locator for accounts/{accountId}, which returns an AccountResource, and that has a locator for transactions returning a TransactionsResource. The important detail is how the child is created. If I just return new AccountResource, the runtime never injects it, so its @Context and CDI fields are null.

I would use ResourceContext, either getResource with the class or initResource on an instance I built, so injection works.

The real win is authorization: the account locator loads the account scoped to the current user and returns 404 if it is missing or belongs to someone else, so every nested endpoint gets a checked object instead of 40 methods each repeating the check.

The trade-offs are that locators are resolved at runtime, so generated OpenAPI or WADL can be incomplete, and deep nesting ties URLs to the object graph.

So I would probably keep one level of nesting where the parent genuinely scopes the child, and for deeper levels consider flat roots like /accounts/{accountId}/transactions with a shared ownership check, which are easier to document and version.

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
Jakarta EE specifications
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 "An API exposes /customers/{id}/accounts/{accountId}/transactions with many nested...": https://scrivio-interview-library.netlify.app/java/jax-rs/1f9579c75c/