EE8 AppDev — Chapter 10: RESTful Web Services with JAX-RS¶
Book: Java EE 8 Application Development — David R. Heffelfinger (Packt)
Chapter: 10 — RESTful Web Services with JAX-RS
Cross-referenced: Day 04 (ExceptionMapper section) · Day 05 (Filters section)
Introduction to RESTful Web Services¶
RESTful web services are flexible and lightweight, typically producing/consuming JSON or XML. JAX-RS defines the Jakarta EE standard for building REST services using annotation-driven POJO classes — zero XML required.
HTTP methods supported by JAX-RS:
| Annotation | HTTP Method | Conventional Use |
|---|---|---|
@GET |
GET | Retrieve an existing resource (safe + idempotent) |
@POST |
POST | Update an existing resource |
@PUT |
PUT | Create a new resource |
@DELETE |
DELETE | Delete an existing resource |
EE8 book HTTP semantics
The EE8 AppDev book lists POST=update, PUT=create — which is the opposite of REST conventions in most modern APIs. In practice (and per RFC 7231): POST=create, PUT=full replace. Follow RFC 7231 in your labs.
Developing a Simple RESTful Web Service¶
Resource Class with @Path¶
@Path defines the URI for the resource. HTTP verb annotations decorate methods:
@Path("customer")
public class CustomerResource {
@GET
@Produces("text/xml")
public String getCustomer() {
return "<customer><id>123</id><firstName>Ahmed</firstName></customer>";
}
@PUT
@Consumes("text/xml")
public void createCustomer(String customerXML) {
System.out.println("Creating customer: " + customerXML);
}
@POST
@Consumes(MediaType.TEXT_XML)
public void updateCustomer(String customerXML) {
System.out.println("Updating customer: " + customerXML);
}
@DELETE
@Consumes("text/xml")
public void deleteCustomer(String customerXML) {
System.out.println("Deleting customer: " + customerXML);
}
}
Configuring the Application Path — @ApplicationPath¶
@ApplicationPath("resources") // base URI = /appName/resources/
public class JaxRsConfig extends Application {}
This is the portable way — no web.xml needed.
JAXB — Automatic Java ↔ XML Conversion¶
Decorate entities with @XmlRootElement for transparent JAXB marshaling:
@XmlRootElement
public class Customer implements Serializable {
private Long id;
private String firstName;
private String lastName;
// constructors, getters, setters
}
Method signatures change from String to Customer — JAX-RS runtime handles serialization automatically:
@GET @Produces("text/xml")
public Customer getCustomer() {
return new Customer(123L, "Ahmed", "Ashour");
}
@PUT @Consumes("text/xml")
public void createCustomer(Customer customer) {
// customer is fully populated by JAX-RS
}
:material-parameter: Query Parameters — @QueryParam¶
Extract URL query string values:
@GET @Produces("text/xml")
public Customer getCustomer(@QueryParam("id") Long id) {
return repo.findById(id);
}
// URL: GET /resources/customer?id=123
Via JAX-RS Client API¶
Client client = ClientBuilder.newClient();
Customer customer = client
.target("http://localhost:8080/app/resources/customer")
.queryParam("id", 1L)
.request()
.get(Customer.class);
Path Parameters — @PathParam¶
Embed variable segments in the URI template and extract via @PathParam:
@GET
@Produces("text/xml")
@Path("{id}/") // URI template: /customer/123/
public Customer getCustomer(@PathParam("id") Long id) {
return repo.findById(id);
}
Via Client API¶
Customer c = client
.target("http://localhost:8080/app/resources/customer")
.path("{id}")
.resolveTemplate("id", 1L)
.request()
.get(Customer.class);
All Parameter Annotations (Full Table)¶
| Annotation | Source | Example |
|---|---|---|
@PathParam |
URI template variable | /users/{id} |
@QueryParam |
URL query string | ?page=2&limit=10 |
@HeaderParam |
HTTP request header | X-Client-Id: dashboard |
@FormParam |
HTML form POST body | username=ahmed |
@CookieParam |
HTTP Cookie header | sessionId=abc123 |
@BeanParam |
Bundles multiple params into a POJO | — |
@DefaultValue |
Fallback value | @DefaultValue("10") int limit |
Content Negotiation — @Produces and @Consumes¶
JAX-RS matches client Accept and Content-Type headers to @Produces/@Consumes:
@GET @Path("{id}/summary")
@Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
public Response getSummary(@PathParam("id") String id) {
TelemetryReport report = repo.findById(id).orElseThrow();
// JAX-RS picks JSON or plain text based on client's Accept header
return Response.ok(report.toTextSummary()).build();
}
Response Builder API¶
The Response class provides a fluent builder for complete HTTP responses:
// 200 OK with entity
Response.ok(entity).build()
// 201 Created with Location header
URI location = uriInfo.getAbsolutePathBuilder().path(id).build();
Response.created(location).build()
// 204 No Content
Response.noContent().build()
// 404 Not Found with entity
Response.status(Response.Status.NOT_FOUND)
.entity(new ErrorBody("Resource not found"))
.type(MediaType.APPLICATION_JSON)
.build()
// Custom status code
Response.status(409).entity(conflictBody).build()
:material-client-service: JAX-RS Client API¶
A complete programmatic HTTP client without raw sockets:
public void insertCustomer(Customer customer) {
Client client = ClientBuilder.newClient();
client.target("http://localhost:8080/app/resources/customer")
.request()
.put(Entity.entity(customer, "text/xml"), Customer.class);
}
// Building fluent request chain:
Response response = client
.target(baseUrl)
.path("customer/{id}")
.resolveTemplate("id", 123L)
.request(MediaType.APPLICATION_JSON)
.header("X-Client-Id", "dashboard")
.get();
int status = response.getStatus();
Customer result = response.readEntity(Customer.class);
Server-Sent Events (SSE)¶
JAX-RS 2.1 (Jakarta EE 8) introduced standard SSE support for server-push streaming:
@Path("serversentevents")
public class SseResource {
@GET
@Produces(MediaType.SERVER_SENT_EVENTS)
public void streamEvents(@Context SseEventSink sink, @Context Sse sse) {
Executor executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
for (float value : getStockValues()) {
try {
TimeUnit.SECONDS.sleep(5);
OutboundSseEvent event = sse.newEventBuilder()
.name("stock-ticker")
.data(String.class, String.format("%.2f", value))
.build();
sink.send(event); // push to connected client
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
sink.close();
});
}
}
JavaScript Client for SSE¶
<script>
function subscribeToStock() {
var source = new EventSource("/app/resources/serversentevents");
source.addEventListener("stock-ticker", function(event) {
document.getElementById("price").textContent = event.data;
}, false);
}
</script>
SSE Deep Dive in Week 3
Full SSE coverage (broadcast channels, SseBroadcaster, live JVM metric streaming) is in Week 3 — Day 17.
Key Takeaways¶
@ApplicationPathon anApplicationsubclass = zero-XML JAX-RS configuration@Produces/@Consumesenable content negotiation — JAX-RS picks the best match from clientAcceptheader@PathParamextracts from URI template;@QueryParamfrom query string;@HeaderParamfrom request headersResponsebuilder gives complete control over status code, headers, and entity body- JAX-RS Client API provides a fluent, type-safe HTTP client without raw
HttpURLConnection - SSE (
SseEventSink) enables server-initiated push — client opens one long-lived HTTP connection and receives streamed events
Back to Day 03 Index | ExceptionMapper section used in Day 04