Unravel โบ Java concepts โบ APIs and web
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
The same switchboard as in Spring, built to a different industry standard.
- 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@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
- 3Return a POJO for 200, or a Response (Response.created(uri).entity(x).build()) to control the status and headers
- 4Errors: throw WebApplicationException subclasses (NotFoundException) or register an ExceptionMapper<T> to convert exceptions into responses in one place
- Filters and interceptors (ContainerRequestFilter, ContainerResponseFilter) are the JAX-RS counterpart of servlet filters, used for authentication, CORS and logging
- 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
- At deployment the runtime (Jersey, RESTEasy, CXF) scans for @Path classes and builds a routing tree from the path templates
@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(); }}- line 1 uses Path, the core of JAX-RS (Jakarta REST) resources
- line 2 uses Produces, the core of JAX-RS (Jakarta REST) resources
- line 7 uses GET, the core of JAX-RS (Jakarta REST) resources
- line 8 uses Path, the core of JAX-RS (Jakarta REST) resources
- line 9 uses Path, the core of JAX-RS (Jakarta REST) resources
- 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:
- Annotations:
@Path@GET@POST@PUT@DELETE@PATCH@Produces@Consumes@PathParam@QueryParam@HeaderParam@FormParam@Context@Providerand 1 more - Types:
WebApplicationExceptionExceptionMapperContainerRequestFilterContainerResponseFilterUriInfoClientBuilderWebTargetResteasyClient
Part of a learning path
- Jakarta EE and other frameworks ยท previous: EJB session beans and container services ยท next: Quarkus applications
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.