BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ APIs and web

I/O and data

JAX-RS (Jakarta REST) resources

JAX-RS, now Jakarta REST, is the standard annotation model for REST in Java EE and Jakarta EE servers and in Quarkus.

Learn these first

At a glance

Mental model

The same switchboard as in Spring, built to a different industry standard.

What happens, in order
  1. 1@Path("/orders") on a class sets the base route; @GET, @POST on methods choose the HTTP verb; @Path("{id}") adds a sub-path and @PathParam("id") reads it@QueryParam reads a query parameter.
  2. 2@Consumes(MediaType.APPLICATION_JSON) and @Produces(...) select the content types; a message body writer (JSON-B or Jackson) converts objects to and from the body
  3. 3Return a POJO for 200, or a Response (Response.created(uri).entity(x).build()) to control the status and headers
  4. 4Errors: throw WebApplicationException subclasses (NotFoundException) or register an ExceptionMapper<T> to convert exceptions into responses in one place
Remember
Watch out
Mixing javax and jakarta namespacesTreating resources as singletonsReturning raw exceptions instead of mapped responsesMissing @Produces so content negotiation surprises clients
Example (highlighted lines explain it)
@Path("/orders")@Produces(MediaType.APPLICATION_JSON)public class OrderResource {    @Inject    OrderService service;    @GET    @Path("{id}")    public Order get(@PathParam("id") long id) {        return service.find(id).orElseThrow(NotFoundException::new);    }    @POST    @Consumes(MediaType.APPLICATION_JSON)    public Response create(OrderRequest request, @Context UriInfo uri) {        Order created = service.create(request);        return Response.created(uri.getAbsolutePathBuilder().path("" + created.id()).build()).entity(created).build();    }}
  1. line 1 uses Path, the core of JAX-RS (Jakarta REST) resources
  2. line 2 uses Produces, the core of JAX-RS (Jakarta REST) resources
  3. line 7 uses GET, the core of JAX-RS (Jakarta REST) resources
  4. line 8 uses Path, the core of JAX-RS (Jakarta REST) resources
  5. line 9 uses Path, the core of JAX-RS (Jakarta REST) resources
  6. line 13 uses POST, the core of JAX-RS (Jakarta REST) resources
Read the full explanation (about 1 min)

JAX-RS, now Jakarta REST, is the standard annotation model for REST in Java EE and Jakarta EE servers and in Quarkus. It looks like Spring MVC with different names: @Path, @GET, @Produces.

A mental model

The same switchboard as in Spring, built to a different industry standard. The same calls get routed, but the buttons carry other labels.

How it works

@Path("/orders") on a class sets the base route; @GET, @POST on methods choose the HTTP verb; @Path("{id}") adds a sub-path and @PathParam("id") reads it. @QueryParam reads a query parameter.

@Consumes(MediaType.APPLICATION_JSON) and @Produces(...) select the content types; a message body writer (JSON-B or Jackson) converts objects to and from the body.

Return a POJO for 200, or a Response (Response.created(uri).entity(x).build()) to control the status and headers.

Errors: throw WebApplicationException subclasses (NotFoundException) or register an ExceptionMapper<T> to convert exceptions into responses in one place.

Going deeper

Filters and interceptors (ContainerRequestFilter, ContainerResponseFilter) are the JAX-RS counterpart of servlet filters, used for authentication, CORS and logging. @Context injects UriInfo, SecurityContext and request objects.

Resource classes are request-scoped by default in most implementations (a new instance per request), unlike Spring singletons, so fields are not shared state unless you mark the class @Singleton.

Packages changed from javax.ws.rs to jakarta.ws.rs with Jakarta EE 9. Mixing the two namespaces on one classpath causes confusing failures.

Under the hood

At deployment the runtime (Jersey, RESTEasy, CXF) scans for @Path classes and builds a routing tree from the path templates. For each request it matches the path, then the method, then content negotiation, injects parameters from the request, calls the resource method and writes the entity with the matching writer.

Common mistakes

  • Mixing javax and jakarta namespaces.
  • Treating resources as singletons.
  • Returning raw exceptions instead of mapped responses.
  • Missing @Produces so content negotiation surprises clients.

How Unravel recognises it

When you paste code, Unravel looks for these signs of jax-rs (jakarta rest) resources. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is JAX-RS (Jakarta REST) resources?

JAX-RS, now Jakarta REST, is the standard annotation model for REST in Java EE and Jakarta EE servers and in Quarkus. It looks like Spring MVC with different names: @Path, @GET, @Produces.

What are the most common mistakes with JAX-RS (Jakarta REST) resources?

Mixing javax and jakarta namespaces. Treating resources as singletons. Returning raw exceptions instead of mapped responses. Missing @Produces so content negotiation surprises clients.

How can I see JAX-RS (Jakarta REST) resources in real code?

Paste your own Java into Unravel and it will point out where jax-rs (jakarta rest) resources appears, explain how it works at the detail level you choose, and draw the flow of the code around it.