REST is not a protocol or a standard — it is a set of constraints that, when followed, produce scalable, simple, and maintainable web services.
GET /api/students — list all students
GET /api/students/42 — get student with ID 42
POST /api/students — create a new student
| # | Constraint | What It Means |
|---|---|---|
| 1 | Client–Server | UI and data storage are separated — they evolve independently |
| 2 | Stateless | Each request contains all the information the server needs — no session state stored on the server |
| 3 | Cacheable | Responses declare whether they can be cached to improve performance |
| 4 | Uniform Interface | Resources are identified by URIs; standard HTTP methods apply uniformly |
| 5 | Layered System | Client cannot tell if it is talking directly to the server or through a proxy/gateway |
| 6 | Code on Demand (optional) | Server can send executable code (e.g., JavaScript) to the client |
REST maps standard HTTP methods to database-style CRUD operations on resources:
| HTTP Method | CRUD Operation | Idempotent? | Example |
|---|---|---|---|
| GET | Read | Yes | GET /api/orders/7 |
| POST | Create | No | POST /api/orders |
| PUT | Update (full) | Yes | PUT /api/orders/7 |
| DELETE | Delete | Yes | DELETE /api/orders/7 |
| PATCH | Update (partial) | No* | PATCH /api/orders/7 |
POST /api/products three times with the same body. How many new products are likely created?| Annotation | Purpose | Example |
|---|---|---|
| @Path | Maps a class or method to a URI path | @Path("/students") |
| @GET | Handles HTTP GET requests | Retrieve resource(s) |
| @POST | Handles HTTP POST requests | Create a resource |
| @PUT | Handles HTTP PUT requests | Update a resource |
| @DELETE | Handles HTTP DELETE requests | Delete a resource |
| @Produces | Specifies response media type | @Produces("application/json") |
| @Consumes | Specifies request media type | @Consumes("application/json") |
| @PathParam | Extracts a value from the URI | @PathParam("id") |
| @QueryParam | Extracts a query string parameter | @QueryParam("page") |
A complete JAX-RS resource class that returns a list of students as JSON:
@Path("/students")
@Produces(MediaType.APPLICATION_JSON)
public class StudentResource {
@Inject
private StudentService service;
@GET // GET /api/students
public List<Student> getAll() {
return service.findAll();
}
@GET
@Path("/{id}") // GET /api/students/42
public Student getById(@PathParam("id") Long id) {
return service.find(id);
}
@POST // POST /api/students
@Consumes(MediaType.APPLICATION_JSON)
public Response create(Student s) {
service.save(s);
return Response.status(201).entity(s).build();
}
}
Used to identify a specific resource.
// URL: /api/students/42
@GET
@Path("/{id}")
public Student get(
@PathParam("id") Long id
) { ... }
The {id} in the path is mandatory — the URL won't match without it.
Used for optional filtering, sorting, pagination.
// URL: /api/students?major=IT&page=2
@GET
public List<Student> search(
@QueryParam("major") String m,
@QueryParam("page")
@DefaultValue("1") int p
) { ... }
Query params are optional — the method still works if omitted.
/api/orders?@Path maps classes and methods to URI paths. @RequestMapping is a Spring annotation — not Jakarta EE.@GET @Path("/{id}"). What does @PathParam("id") do on the method parameter?@PathParam binds the {id} template variable in the URI to the Java method parameter.Jakarta JSON Binding (JSON-B) converts Java objects to JSON and back with zero configuration:
// Java object
Student s = new Student();
s.setName("Alice");
s.setGpa(3.8);
// Becomes JSON automatically:
{
"name": "Alice",
"gpa": 3.8
}
The client tells the server what format it wants via the Accept header:
Accept: application/json
Accept: application/xml
The server responds in the matching format if supported.
The Response class gives you fine-grained control over what the server sends back:
@POST
public Response create(Student s) {
service.save(s);
URI uri = URI.create("/api/students/" + s.getId());
return Response.created(uri) // 201 Created
.entity(s) // response body
.build();
}
@DELETE @Path("/{id}")
public Response delete(@PathParam("id") Long id) {
service.remove(id);
return Response.noContent().build(); // 204 No Content
}
| Code | Meaning | When to Use |
|---|---|---|
| 200 OK | Request succeeded | GET, PUT success |
| 201 Created | New resource created | POST success |
| 204 No Content | Success, no body | DELETE success |
| 400 Bad Request | Invalid input | Validation failure |
| 404 Not Found | Resource doesn't exist | ID not in database |
| 500 Internal Error | Server-side failure | Unhandled exception |