Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Write a Jakarta REST (JAX-RS) resource that returns an order by id as JSON, and name two implementations.

A Jakarta REST resource is a class annotated @Path("/orders") with a method annotated @GET, @Path("/{id}") and @Produces(MediaType.APPLICATION_JSON) that takes @PathParam("id") long id and returns the order, or a Response with status 404 when it does not exist. Two implementations are Jersey and RESTEasy, and Spring Boot supports Jersey through spring-boot-starter-jersey.

Why this question is asked

Jakarta REST, still widely known by its old name JAX-RS, is the standard Java API for building REST endpoints. It is what we use in Jakarta EE application servers, in MicroProfile runtimes such as Open Liberty, and in Quarkus, and it can also run inside Spring Boot. Interviewers ask for a small resource because it shows whether we know the annotations, how parameters are bound, and how to return an error status properly. In this answer, we will recap the main pieces of Jakarta REST, write the resource step by step, handle the not-found case two ways, see how JSON is produced, run it in Spring Boot with Jersey, and name the implementations.

1A Quick Recap of Jakarta REST

Jakarta REST lives in the jakarta.ws.rs packages. A resource class is a plain class whose methods handle HTTP requests. @Path gives the URL path, on the class for a common prefix and on methods for the rest. @GET, @POST, @PUT and @DELETE choose the HTTP method. @Produces says which media types a method can return, and @Consumes which it accepts. Parameters are bound from the request with @PathParam, @QueryParam, @HeaderParam and similar annotations. An Application subclass, usually annotated @ApplicationPath("/api"), sets the base path for all resources.

The API is a specification only. To run it we need an implementation, which routes requests to our methods and converts objects to and from JSON.

2Writing the Resource

We start with a simple Order record and a repository that returns an Optional. The resource returns the order when it exists and a 404 Not Found response when it does not.

Java
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
public record Order(long id, String customer, java.math.BigDecimal total) {}
@Path("/orders")
public class OrderResource {
private final OrderRepository repository;
@jakarta.inject.Inject
public OrderResource(OrderRepository repository) { this.repository = repository; }
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response findById(@PathParam("id") long id) {
return repository.findById(id)
.map(order -> Response.ok(order).build()) // 200 + JSON body
.orElseGet(() -> Response.status(Response.Status.NOT_FOUND).build()); // 404
}
}
// GET /api/orders/7 -> 200 {"id":7,"customer":"Asha","total":42.50}
// GET /api/orders/99 -> 404 (empty body)
// GET /api/orders/abc -> 404, because "abc" cannot be converted to long

Notice how the path is assembled: the class-level @Path("/orders") combines with the method's @Path("/{id}"), and {id} is a template variable that @PathParam("id") binds to the method parameter. Jakarta REST converts the text to a long for us. If the conversion fails, as with abc, the implementation answers 404, because a path parameter that cannot be parsed means the path does not match.

3Two Ways to Handle Not Found

Returning a Response gives full control over status and headers. The alternative is to return the Order type directly and throw jakarta.ws.rs.NotFoundException when it is missing. The implementation turns any WebApplicationException into the matching HTTP status, so the method stays short.

Java
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Order findById(@PathParam("id") long id) {
return repository.findById(id)
.orElseThrow(() -> new NotFoundException("Order " + id + " not found"));
}
// GET /api/orders/99 -> 404

Both are correct. Returning Order directly documents the success type better, which helps OpenAPI generators. For a consistent error body across the API, such as a JSON problem object, we register an ExceptionMapper<NotFoundException> that builds that body in one place.

4How the Order Becomes JSON

@Produces(MediaType.APPLICATION_JSON) tells the implementation that the method returns JSON. The actual conversion is done by a message body writer, a provider that knows how to write a Java object to the response stream. Jakarta EE runtimes include JSON Binding (JSON-B, for example Yasson), and Jersey and RESTEasy also have Jackson modules. If no JSON provider is on the classpath, the request fails with a 500 error saying no message body writer was found for the type, which is a common first-run surprise.

Step throughGET /api/orders/7 through Jakarta REST
ClientJerseyOrderResourceJSON provider
GET /api/orders/7, Accept: application/json
match @Path /orders/{id}, convert "7" to long
findById(7)
Response.ok(order)
write Order as application/json
200 {"id":7,...}

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

5Running It in Spring Boot With Jersey

Spring Boot supports Jersey through spring-boot-starter-jersey, which brings Jersey and its Jackson JSON support. We register resources in a ResourceConfig bean. If the resource is also a Spring @Component, Spring injects its dependencies through the constructor, so we can use @Autowired or plain constructor injection instead of @Inject.

XML
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
@Component
@ApplicationPath("/api")
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
register(OrderResource.class);
}
}
// OrderResource annotated @Component so Spring builds it with OrderRepository

We can also set the base path with spring.jersey.application-path=/api instead of @ApplicationPath. Running Jersey and Spring MVC in the same application is possible but confusing, so most services pick one.

6Implementations to Name

Eclipse Jersey is the reference implementation, used by GlassFish and Payara and supported by Spring Boot. RESTEasy, from Red Hat, is used by WildFly and was the basis of Quarkus's REST layer (Quarkus REST, formerly RESTEasy Reactive). Apache CXF also implements Jakarta REST, and Open Liberty ships its own implementation based on CXF. Because they all implement the same specification, the resource above runs on any of them with only configuration changes.

7Follow-up questions

What is the difference between @PathParam and @QueryParam?

@PathParam binds a part of the URL path, such as 7 in /orders/7, and is used to identify a resource. @QueryParam binds a parameter after the question mark, such as status in /orders?status=OPEN, and is used for filtering, sorting or paging. Query parameters are optional by default, so we can add @DefaultValue for a fallback.

How would you return 201 Created for a POST?

Build the response with Response.created(uri).entity(order).build(), where uri is the location of the new resource, usually created with UriInfo: uriInfo.getAbsolutePathBuilder().path(String.valueOf(order.id())).build(). The client receives status 201 with a Location header.

How do you test a Jakarta REST resource?

Jersey has a test framework that starts the application on an in-memory or lightweight container, and in Spring Boot we can use @SpringBootTest with a random port and call the endpoint with a REST client. Pure unit tests can also call the resource method directly with a fake repository.

8Conclusion

In this answer, we wrote a Jakarta REST resource with @Path("/orders") on the class and @GET, @Path("/{id}") and @Produces(MediaType.APPLICATION_JSON) on the method, bound the id with @PathParam, and returned either the order or a 404, through Response or NotFoundException. We saw that JSON needs a provider such as Jackson or JSON-B, that Spring Boot runs Jersey through spring-boot-starter-jersey, and that Jersey and RESTEasy are the best-known implementations.

What to say in the interviewAbout 70 seconds aloud

Open with

I would write a class annotated @Path("/orders") and a method annotated @GET, @Path("/{id}") and @Produces(MediaType.APPLICATION_JSON). The method takes @PathParam("id") long id, so Jakarta REST pulls the id out of the URL and converts it to a long; if it cannot convert, the path does not match and the client gets a 404.

Then, if they want more

Inside, I look up the order in the repository, and if it exists I return Response.ok(order).build(), which gives a 200 with the order as JSON; otherwise Response.status(NOT_FOUND).build(). An equally good option is to return Order directly and throw NotFoundException, which the runtime maps to 404, and add an ExceptionMapper if I want a consistent error body.

The JSON conversion itself comes from a message body writer, Jackson or JSON-B, so one of those has to be on the classpath. For implementations, Eclipse Jersey is the reference implementation, and RESTEasy from Red Hat is used by WildFly and Quarkus; Apache CXF is another.

In Spring Boot I would add spring-boot-starter-jersey, which uses Jersey with Jackson, register the resource in a ResourceConfig bean, and make the resource a Spring component so its repository is injected.

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 (JAX-RS) resource that returns an order by id as JSON, and name two...": https://scrivio-interview-library.netlify.app/java/jakarta-ee-platform/59fdc01dc8/