Preface

Author’s Note

The bulk of my career has been spent working with and implementing distributed middleware. In the mid-’90s I worked for the parent company of Open Environment Corporation, working on DCE tools. Later on, I worked for Iona, developing their next-generation CORBA ORB. Currently, I work for the JBoss division of Red Hat, which is entrenched in Java middleware, specifically Java EE. So, you could say that I have a pretty rich perspective when it comes to middleware.

I must tell you that I was originally very skeptical of REST as a way of writing SOA applications. It seemed way too simple and shortsighted, so I sort of blew it off for a while. One day though, back in mid-2007, I ran into my old Iona boss and mentor Steve Vinoski while grabbing a sandwich at D’Angelo in Westford, MA near Red Hat’s offices. We ended up sitting down, having lunch, and talking for hours. The first shocker for me was that Steve had left Iona to go work for a start-up. The second was when he said, “Bill, I’ve abandoned CORBA and WS-* for REST.” For those of you who don’t know Steve, he contributed heavily to the CORBA specification, wrote a book on the subject (which is basically the CORBA bible), and is a giant in the distributed computing field, writing regularly for C++ Report and IEEE. How could the guy I looked up to and was responsible for my foundation in distributed computing abandon CORBA, WS-*, and the distributed framework landscape he was instrumental in creating? I felt a little betrayed and very unnerved (OK, maybe I’m exaggerating a little…).

We ended up arguing for a few hours on which was better, WS-*/CORBA or REST. This conversation spilled into many other lengthy email messages, with me trying to promote WS-* and him defending REST. The funniest thing to me was that as I researched REST more and more I found that my arguments with Steve were just another endless replay of debates that had been raging across the blogosphere for years. They are still raging to this day.

Anyway, it took months for me to change my mind and embrace REST. You would figure that my distributed computing background was an asset, but it was not. DCE, CORBA, WS-*, and Java EE were all baggage. All were an invisible barrier for me to accept REST as a viable (and better) alternative for writing SOA applications. I think that’s what I liked most about REST. It required me to rethink and reformulate the foundation of my distributed computing knowledge. Hopefully your journey isn’t as difficult as mine and you will be a little less stubborn and more open-minded than I was.

Who Should Read This Book

This book teaches you how to design and develop distributed web services in Java using RESTful architectural principles on top of the HTTP protocol. It is mostly a comprehensive reference guide on the JAX-RS specification, which is a JCP standardized annotation framework for writing RESTful web services in Java.

While this book does go into many of the fundamentals of REST, it does not cover them all and focuses more on implementation rather than theory. You can satisfy your craving for more RESTful theory be obtaining RESTful Web Services by Leonard Richardson and Sam Ruby (O’Reilly). If you are familiar writing Java EE applications, you will be very comfortable reading this book. If you are not, you will be at a disadvantage, but some experience with web application development, HTTP, and XML is a huge plus. Before reading this book, you should also be fairly fluent in the Java language and specifically know how to use and apply Java annotations. If you are unfamiliar with the Java language, I recommend Learning Java by Patrick Niemeyer and Jonathan Knudsen (O’Reilly).

How This Book Is Organized

This book is organized into two parts: the technical manuscript, followed by the JAX-RS workbook. The technical manuscript explains what REST and JAX-RS are, how they work, and when to use them. The JAX-RS workbook provides step-by-step instructions for installing, configuring, and running the JAX-RS examples from the manuscript with the JBoss RESTEasy framework, an implementation of JAX-RS.

Part I, REST and the JAX-RS Standard

Part I starts off with a brief introduction to REST and HTTP. It then guides you through the basics of the JAX-RS specification, and then in later chapters shows you how you can apply JAX-RS to build RESTful web services:

Chapter 1, Introduction to REST

This chapter gives you a brief introduction to REST and HTTP.

Chapter 2, Designing RESTful Services

This chapter walks you through the design of a distributed RESTful interface for an e-commerce order entry system.

Chapter 3, Your First JAX-RS Service

This chapter walks you through the development of a RESTful web service written in Java as a JAX-RS service.

Chapter 4, HTTP Method and URI Matching

This chapter defines how HTTP requests are dispatched in JAX-RS and how you can use the @Path annotation and subresources.

Chapter 5, JAX-RS Injection

This chapter walks you through various annotations that allow you to extract information from an HTTP request (URI parameters, headers, query parameters, form data, cookies, matrix parameters, encoding, and defining default values).

Chapter 6, JAX-RS Content Handlers

This chapter explains how to marshal HTTP message bodies to and from Java objects using built-in handlers or writing your own custom marshallers.

Chapter 7, Response Codes, Complex Responses, and Exception Handling

This chapter walks through the JAX-RS Response object and how you use it to return complex responses to your client (ResponseBuilder). It also explains how exception and error handling work in JAX-RS.

Chapter 8, HTTP Content Negotiation

This chapter explains how HTTP content negotiation works, its relationship to JAX-RS, and how you can leverage this within RESTful architectures.

Chapter 9, HATEOAS

This chapter dives into Hypermedia As The Engine Of Application State and how it relates to JAX-RS (UriInfo and UriBuilder).

Chapter 10, Scaling JAX-RS Applications

This chapter explains how you can increase the performance of your services by leveraging HTTP caching protocols. It also shows you how to manage concurrency conflicts in high-volume sites.

Chapter 11, Deployment and Integration

This chapter explains how you can deploy and integrate your JAX-RS services within Java Enterprise Edition, servlet containers, EJB, Spring, and JPA.

Chapter 12, Securing JAX-RS

This chapter walks you through the most popular mechanisms to perform authentication on the Web. It then shows you how to implement secure applications with JAX-RS.

Chapter 13, RESTful Java Clients

This chapter shows you how to write RESTful clients in Java using various libraries and frameworks as an example (java.net.URL, Apache HTTP Client, and RESTEasy).

Chapter 14, JAX-RS Implementations

This chapter provides a short review of available JAX-RS implementations.

Part II, JAX-RS Workbook

The JAX-RS workbook shows you how to execute examples from chapters in the book that include at least one significant example. You’ll want to read the introduction to the workbook to set up RESTEasy and configure it for the examples. After that, just go to the workbook chapter that matches the chapter you’re reading. For example, if you are reading Chapter 3 on writing your first JAX-RS service, use Chapter 16, Examples for Chapter 3, of the workbook to develop and run the examples with RESTEasy.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, filenames, and file extensions

Constant width

Indicates variables, method names, and other code elements, as well as the contents of files

Constant width bold

Highlights new code in an example

Constant width italic

Shows text that should be replaced with user-supplied values

Note

This icon signifies a tip, suggestion, or general note.

Warning

This icon indicates a warning or caution.

Using Code Examples

This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “RESTful Java with JAX-RS by Bill Burke. Copyright 2010 William J. Burke, Jr., 978-0-596-15804-0.”

If you feel your use of code examples falls outside fair use or the permission given here, feel free to contact us at .

We’d Like to Hear from You

Every example in this book has been tested, but occasionally you may encounter problems. Mistakes and oversights can occur and we will gratefully receive details of any that you find, as well as any suggestions you would like to make for future editions. You can contact the authors and editors at:

O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at:

http://www.oreilly.com/catalog/9780596158040

To comment or ask technical questions about this book, send email to the following, quoting the book’s ISBN number (9780596158040):

For more information about our books, conferences, Resource Centers, and the O’Reilly Network, see our website at:

http://www.oreilly.com

Safari® Books Online

Note

Safari Books Online is an on-demand digital library that lets you easily search over 7,500 technology and creative reference books and videos to find the answers you need quickly.

With a subscription, you can read any page and watch any video from our library online. Read books on your cell phone and mobile devices. Access new titles before they are available for print, and get exclusive access to manuscripts in development and post feedback for the authors. Copy and paste code samples, organize your favorites, download chapters, bookmark key sections, create notes, print out pages, and benefit from tons of other time-saving features.

O’Reilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from O’Reilly and other publishers, sign up for free at http://my.safaribooksonline.com.

Acknowledgments

First, I’d like to thank Steve Vinoski for introducing me to REST. Without our conversations and arguments, I would never have written this book. Next, I’d like to thank Marc Hadley and Paul Sandoz, the leads of the JAX-RS specification. They ran an excellent expert group and also wrote the Foreword and contributed to Chapter 14. I’d like to thank Sergey Beryozkin for contributing the Apache CXF section. It is cool when competitors can be on good terms with each other. Jeff Mesnil and Michael Musgrove were instrumental in reviewing this book and provided a lot of great feedback. Subbu Allaraju helped tremendously in making sure my understanding and explanation of RESTful theory was correct. By the way, I strongly suggest you check out his blog at www.subbu.org. Heiko Braun helped on the first few chapters as far as reviewing goes. I’d also like to thank the contributors to the RESTEasy project, specifically Solomon Duskis, Justin Edelson, Ryan McDonough, Attila Kiraly, and Michael Brackx. Without them, RESTEasy wouldn’t be where it is. Finally, I’d like to thank Mike Loukides, who has been my editor throughout my O’Reilly career.

Get RESTful Java with JAX-RS now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.