Returning additional metadata with responses

We discussed a few examples in the previous section for retrieving resources via the HTTP GET request. The resource class implementations that we used in these examples were simply returning the plain Java constructs in response to the method call. What if you want to return extra metadata, such as the Cache-Control header or the status code, along with the result (resource representation)?

JAX-RS allows you to return additional metadata via the javax.ws.rs.core.Response class that wraps the entity and additional metadata such as the HTTP headers, HTTP cookie, and status code. You can create a Response instance by using javax.ws.rs.core.Response.ResponseBuilder as a factory. The following example demonstrates ...

Get RESTful Java Web Services - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.