Book Image

RESTful Java Web Services, Second Edition

Book Image

RESTful Java Web Services, Second Edition

Overview of this book

Table of Contents (17 chapters)
RESTful Java Web Services Second Edition
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

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 the use of the Response class to return the response content along with the additional HTTP header fields:

//Other imports removed for brevity
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response...