SkillsHooksPromptsAgentsPersonasModelsPoliciesToolsTemplatesBundlesCategoriesStart here
← Skills
Sk skillbackenddraft

Java Ee Jax Rs

Java EE JAX-RS Resource Creation: Annotations, Response types, JSON mapping, and Exception mapping. For high-density industrial backends.

id skill/java-ee-jax-rsv1.0.0by claudeskills.in communityinvoke /java-ee-jax-rs
backendclaudeskills

System prompt fragment

JAX-RS Resource Implementation

Guide for creating RESTful resources in Java EE (Jakarta EE) using JAX-RS.

Core Annotations

Annotation Purpose Usage
@Path Resource entry point Type or Method level
@GET/POST... HTTP Method Method level
@Produces Output Media Type Type or Method level
@Consumes Input Media Type Type or Method level
@PathParam Extract from path Parameter level
@QueryParam Extract from query Parameter level
@Context Inject Request/Context Parameter level

Example Resource

import jakarta.ws.rs.*;
import jakarta.ws.rs.core.*;

@Path("/users")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class UserResource {

    @GET
    @Path("/{id}")
    public Response getUser(@PathParam("id") String id) {
        // Logic to fetch user
        User user = userService.find(id);
        if (user == null) {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
        return Response.ok(user).build();
    }

    @POST
    public Response createUser(User user, @Context UriInfo uriInfo) {
        // Logic to create user
        String id = userService.save(user);
        UriBuilder builder = uriInfo.getAbsolutePathBuilder();
        builder.path(id);
        return Response.created(builder.build()).entity(user).build();
    }
}

Exception Mapping

Use ExceptionMapper to return clean error responses.

import jakarta.ws.rs.ext.*;
import jakarta.ws.rs.core.*;

@Provider
public class EntityNotFoundMapper implements ExceptionMapper<EntityNotFoundException> {
    @Override
    public Response toResponse(EntityNotFoundException ex) {
        ErrorResponse error = new ErrorResponse("NOT_FOUND", ex.getMessage());
        return Response.status(Response.Status.NOT_FOUND)
            .entity(error)
            .type(MediaType.APPLICATION_JSON)
            .build();
    }
}

JSON Data Mapping

Ensure you have a JSON provider (like Jackson or Yasson) registered. Standard DTO:

public class UserDTO {
    public String id;
    public String name;
    // Getters and setters
}

Best Practices

  1. Use Response return type for full control over headers and status codes.
  2. Follow RESTful naming conventions (plural for resource paths).
  3. Use Transfer Objects (DTOs) instead of exposing entities directly.
  4. Implement ExceptionMappers for consistent error structures.
  5. Annotate classes with @Produces and @Consumes at the type level to avoid repetition.
Author claudeskills.in community. Source claudeskills.in (original ↗). License unknown — the source states none; review before redistributing. Imported 2026-09-03. Aggregated by claudeskills.in from community GitHub lists.