Build a Java endpoint with Jakarta REST: paths, methods, and media types
Jakarta RESTful Web Services (Jakarta REST, formerly JAX-RS) is an API for exposing HTTP resource endpoints by annotating Java classes and methods. @ApplicationPath sets the application base path, @Path identifies resource paths, and @GET/@POST plus @Consumes/@Produces select methods and representation types. This guide uses Jakarta REST 4.0 and Java SE 17 or later to build a minimal mapping and diagnose media-type mismatches and response statuses.
Jakarta RESTful Web Services (Jakarta REST, formerly JAX-RS) is an API for exposing HTTP resource endpoints by annotating Java classes and methods. @ApplicationPath sets the application base path, @Path identifies resource paths, and @GET/@POST plus @Consumes/@Produces select methods and representation types. This guide uses Jakarta REST 4.0 and Java SE 17 or later to build a minimal mapping and diagnose media-type mismatches and response statuses.
Combine application, class, and method paths
The Jakarta REST runtime gets an application base path from @ApplicationPath on an Application subclass or from a servlet mapping. Class-level and method-level @Path values then identify a resource URI. A template such as {id} can be passed to a method with @PathParam. The final address also includes the server address and web application's context root.
For example, combining @ApplicationPath("api"), @Path("notes"), and @Path("{id}") yields /api/notes/{id} after the context root. Leading and trailing slashes can be confusing when composing paths, so use one convention and document the deployed base URI.
@ApplicationPath("api")
public class NotesApplication extends Application {
}
@Path("notes")
@Produces(MediaType.TEXT_PLAIN)
public class NotesResource {
@GET
@Path("{id}")
public Response get(@PathParam("id") String id) {
if (!"42".equals(id)) {
return Response.status(Response.Status.NOT_FOUND).build();
}
return Response.ok("Note 42").build();
}
@POST
@Consumes(MediaType.TEXT_PLAIN)
public Response create(String title, @Context UriInfo uriInfo) {
if (title == null || title.isBlank()) {
return Response.status(Response.Status.BAD_REQUEST).build();
}
String id = noteService.create(title);
URI location = uriInfo.getAbsolutePathBuilder().path(id).build();
return Response.created(location).entity(id).build();
}
}This illustrative skeleton shows routing and response construction. The application must provide noteService with persistence, unique IDs, and concurrency control. The example was not compiled or run in a REST runtime. Account for the default per-request resource lifecycle and any alternative scope chosen by the app; shared objects need safe concurrency handling.
HTTP methods and media types select a resource method
@GET, @POST, @PUT, and @DELETE declare HTTP operations on a resource. The annotated Java method handles that HTTP method. @Consumes limits the request body Content-Type the method accepts; @Produces declares the response representation types that may be selected against the client's Accept header. A class-level declaration supplies a default; a method-level declaration can override it.
In the example, POST /api/notes accepts Content-Type: text/plain and returns 201 Created with the new resource address in the Location header. GET /api/notes/42 returns text/plain. The runtime considers the HTTP method, path, request body type, and Accept header together. A missing resource can yield 404, an unsupported HTTP method 405, no method that accepts the request body type 415, or no method that can produce an acceptable response type 406.
If you want to send JSON objects, confirm that the runtime includes MessageBodyReader and MessageBodyWriter providers for those Java types. The annotations alone do not serialize arbitrary Java objects as JSON. Check that the Jakarta REST implementation and deployment include a JSON provider, then verify with actual Content-Type and Accept headers.
How a resource method becomes an HTTP response
Returning a simple entity lets the runtime map it to the body; the default status is generally 200 when the entity is non-null and 204 when it is null. Returning Response lets the method set status, headers, and entity explicitly. The example's POST uses Response.created(location) to create a 201 response with a Location header. A missing id returns 404, and a blank title returns 400 so the HTTP result reflects the failure condition.
Keep business rules and data boundaries in the resource/application layer. Design cross-cutting concerns such as serialization, validation, and authentication explicitly with providers, filters, or application services. Jakarta REST maps HTTP to Java methods; it does not choose your database schema, authentication policy, transactions, or error format.
Common route and content-negotiation problems
**The expected URI returns 404:** combine the server address, web-app context root, `@ApplicationPath` or servlet mapping, class `@Path`, and method `@Path` to find the deployed path.
**405 Method Not Allowed:** the path matches, but no request-method annotation handles that HTTP method.
**415 Unsupported Media Type:** check whether the request `Content-Type` matches a candidate method's `@Consumes`.
**406 Not Acceptable:** check whether request `Accept` overlaps with `@Produces`. If `Accept` is absent, the client is treated as accepting any media type.
**A JSON body cannot be read or written:** confirm that a message-body provider for the DTO is packaged and that the implementation version and media type match.
**Data leaks between requests:** the default resource instance is per request, but a singleton, CDI, or implementation-specific scope changes shared-state rules. Verify the actual instance scope and thread safety of shared storage.
Scope and sources
[Jakarta RESTful Web Services 4.0 specification](https://jakarta.ee/specifications/restful-ws/4.0/jakarta-restful-ws-spec-4.0.html) — application paths, resource lifecycle, HTTP methods, media selection, and response status
[Jakarta RESTful Web Services 4.0 API docs](https://jakarta.ee/specifications/restful-ws/4.0/apidocs/) — annotation-based resources and API overview
[Jakarta EE Tutorial: Building RESTful Web Services with Jakarta REST](https://jakarta.ee/learn/docs/jakartaee-tutorial/current/websvcs/rest/rest.html) — resource classes, URI templates, and application paths
[Jakarta REST 4.0 release page](https://jakarta.ee/specifications/restful-ws/4.0/) — Jakarta EE 11 release, Java SE minimum, and API artifact
Scope: Jakarta REST 4.0, minimum platform Java SE 17. JSON providers and container deployment details vary by implementation. Official sources were read on 2026-11-20. Code is illustrative and was not compiled, deployed, or verified with HTTP requests.