Quick revision
The short answer
We put @Path("orders") on the class, use @GET @Path("{id: \\d+}") with @PathParam("id") long id so a non-numeric id simply does not match and gives 404, and read the list filters with @QueryParam("status") and @QueryParam("limit") @DefaultValue("20") int limit. The POST method consumes JSON, builds the new order's URI with uriInfo.getAbsolutePathBuilder().path(String.valueOf(id)).build(), and returns Response.created(uri).entity(dto).build(), which sends 201 with a Location header. Resource classes are per-request by default, so request data can live in fields, while services are injected.
What to remember
In this answer, we wrote an OrderResource with @Path("orders"), a numeric {id: \d+} template so non-numeric ids give 404, query parameters with a default limit, and a POST that returns 201 Created with a Location built from UriInfo. We saw why Response.created is needed instead of returning the entity, why conversion failures on query parameters give 404, and why per-request resources can hold request data in fields while services are injected.
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 put @Path("orders") on the class, use @GET @Path("{id: \\d+}") with @PathParam("id") long id so a non-numeric id simply does not match and gives 404, and read the list filters with @QueryParam("status") and @QueryParam("limit") @DefaultValue("20") int limit. The POST method consumes JSON, builds the new order's URI with uriInfo.getAbsolutePathBuilder().path(String.valueOf(id)).build(), and returns Response.created(uri).entity(dto).build(), which sends 201 with a Location header. Resource classes are per-request by default, so request data can live in fields, while services are injected.
Why this question is asked
A small CRUD resource is the most common thing we write with JAX-RS, and it shows quickly whether we know the details that make an API correct: numeric path constraints, default values, the right status code for creation and the Location header. Interviewers like this exercise because each line has a reason. In this answer, we will recap the annotations, write the whole resource, look at each method and why it is written that way, and then answer the follow-up questions about failed conversions and missing orders.
1A Quick Recap of the Annotations
Jakarta REST, the current name for JAX-RS, maps HTTP requests to Java methods with annotations in the jakarta.ws.rs package. @Path sets the URL, @GET and @POST choose the HTTP method, @PathParam reads a template variable from the path, @QueryParam reads a query string parameter and @DefaultValue gives a value when it is missing. @Produces and @Consumes declare the media types the method writes and reads. @Context injects runtime objects such as UriInfo, which describes the current request URL.
For POST, HTTP says that a request that creates a resource should answer 201 Created with a Location header holding the new resource's URL. Returning the object directly from the method gives 200 OK and no Location, so we return a Response instead.
2Writing the Resource
Here is the full class. OrderService is a CDI bean that does the real work, and OrderDto and OrderRequest are simple records.
3Why the id Has a Regular Expression
The template {id: \d+} accepts only digits. In Java source the backslash inside a string literal must be escaped, so the annotation is written with two backslashes, and the runtime sees the regular expression \d+, one or more digits.
The reason for the constraint is what happens without it. With a plain {id} and a long parameter, GET /orders/abc matches the template and then the runtime fails to convert abc to long. The specification says a failed @PathParam conversion also produces a 404, but that NotFoundException passes through our exception mappers, and a broad catch-all mapper can turn it into a 500. With the regex, abc never matches our method at all, and a literal path such as @Path("summary") can sit next to it without any ranking question.
4Query Parameters and Defaults
A missing @QueryParam gives null for object types such as OrderStatus or String, and the default value of the Java type, 0 for int, unless @DefaultValue is present. With @DefaultValue("20"), a missing limit becomes 20. The runtime converts the text using the type's rules: primitives and their wrappers, types with a constructor taking a String, or a static valueOf(String) or fromString(String) method, which is how the enum works. A ParamConverterProvider can add other types.
We also cap the limit at 100 in the method. A default protects us when the client sends nothing, but a client can still send limit=1000000, so an upper bound is part of a safe list endpoint.
5Creating an Order With 201 and Location
uriInfo.getAbsolutePathBuilder() starts from the absolute URL of the current request, such as http://host/api/orders, so path("43") produces http://host/api/orders/43 without our code knowing the host name, port or application context path. Response.created(location) sets status 201 and the Location header, and entity(created) adds the created order as the body, which saves the client a second request.
One more design point: resource classes are created per request by default. That is why it is safe to have the UriInfo in a field, because each request gets its own resource instance. Services, on the other hand, are shared and stateless, and come in by injection, never by new inside the resource.
5 messages in order. Play them, or step through them one at a time.
6Follow-up questions
What happens if a query parameter cannot be converted to int?
For @QueryParam, @PathParam and @MatrixParam, the specification says a conversion failure produces a NotFoundException, which is a 404, while for @HeaderParam and @CookieParam it is a 400. A 404 for limit=abc is surprising for clients, so many teams validate input explicitly, for example by accepting a String or adding Bean Validation constraints such as @Min(1) @Max(100) and mapping violations to 400.
How would you return 404 when the order does not exist?
Throw jakarta.ws.rs.NotFoundException, as in the code, or return Response.status(Response.Status.NOT_FOUND).build(). Throwing works well with Optional.orElseThrow and lets an ExceptionMapper format the error body consistently, for example as application/problem+json.
Where would validation of the POST body go?
Add Bean Validation constraints to OrderRequest and @Valid on the method parameter. A Jakarta EE runtime with Bean Validation then rejects invalid bodies with 400 before our method runs.
7Conclusion
In this answer, we wrote an OrderResource with @Path("orders"), a numeric {id: \d+} template so non-numeric ids give 404, query parameters with a default limit, and a POST that returns 201 Created with a Location built from UriInfo. We saw why Response.created is needed instead of returning the entity, why conversion failures on query parameters give 404, and why per-request resources can hold request data in fields while services are injected.
Open with
I would put @Path("orders") and @Produces JSON on the class. For the single order, I would use @GET with @Path("{id: \\d+}") and a long id from @PathParam. The regex matters: a non-numeric id simply does not match the template, so the client gets a 404, instead of matching and then failing to convert.
Then, if they want more
Inside I call the service and throw NotFoundException when the Optional is empty. For the list, a @GET with no extra path, @QueryParam("status") as an OrderStatus enum, which JAX-RS converts through valueOf, and @QueryParam("limit") with @DefaultValue("20") on an int; a missing status is null and a missing limit is 20, and I would cap the limit at something like 100.
For the POST, @Consumes JSON with an OrderRequest parameter. After creating the order, I build the URI with uriInfo.getAbsolutePathBuilder().path(String.valueOf(id)).build(), and return Response.created(uri).entity(dto).build(), which gives 201 and a Location header; returning the DTO directly would give 200 without Location. UriInfo comes in through @Context, which is fine in a field because resources are per-request by default, while the service is injected.
One detail I would mention: a query parameter that cannot be converted, like limit=abc, gives 404 by the spec, so I might validate it explicitly to return 400.
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 "Write a Jakarta REST resource class for /orders that supports GET /orders/{id} where id...": https://scrivio-interview-library.netlify.app/java/jax-rs/114e325bc7/