Skip to Content
RESTful Java with JAX-RS
book

RESTful Java with JAX-RS

by Bill Burke
November 2009
Intermediate to advanced
310 pages
7h 42m
English
O'Reilly Media, Inc.
Content preview from RESTful Java with JAX-RS

Chapter 23. Examples for Chapter 10

In Chapter 10, you learned about HTTP Caching techniques. Servers can tell HTTP clients if and how long they can cache retrieved resources. Expired caches can be revalidated to avoid resending big messages by issuing conditional GET invocations. Conditional PUT operations can be invoked for safe concurrent updates.

Example ex10_1: Caching and Concurrent Updates

The example in this chapter expands on the CustomerResource example repeated throughout this book to support caching, conditional GETs, and conditional PUTs.

The Server Code

The first thing is to add a hashCode() method to the Customer class:

src/main/java/com/restfully/shop/domain/Customer.java

@XmlRootElement(name = "customer")
public class Customer
{
...
   @Override
   public int hashCode()
   {
      int result = id;
      result = 31 * result + (firstName != null
                                  ? firstName.hashCode() : 0);
      result = 31 * result + (lastName != null
                                  ? lastName.hashCode() : 0);
      result = 31 * result + (street != null
                                  ? street.hashCode() : 0);
      result = 31 * result + (city != null ? city.hashCode() : 0);
      result = 31 * result + (state != null ? state.hashCode() : 0);
      result = 31 * result + (zip != null ? zip.hashCode() : 0);
      result = 31 * result + (country != null
                                    ? country.hashCode() : 0);
      return result;
   }
}

This method is used in the CustomerResource class to generate semiunique ETag header values. While a hash code calculated in this manner isn’t guaranteed to be unique, there is a high probability that it will be. A database application ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

RESTful Java with JAX-RS 2.0, 2nd Edition

RESTful Java with JAX-RS 2.0, 2nd Edition

Bill Burke
RESTful Java Web Services - Third Edition

RESTful Java Web Services - Third Edition

Balachandar Bogunuva Mohanram

Publisher Resources

ISBN: 9780596809300Supplemental ContentErrata Page