You are previewing RESTful Java with JAX-RS.

RESTful Java with JAX-RS

Cover of RESTful Java with JAX-RS by Bill Burke Published by O'Reilly Media, Inc.
O'Reilly logo

Example ex09_2: Link Headers

There are two educational goals I want to get across with this example. The first is the use of Link headers within a RESTful application. The second is that if your services provide the appropriate links, you only need one published URL to navigate through your system. When you look at the client code for this example, you’ll see that only one URL is hardcoded to start the whole process of the example.

To illustrate these techniques, a few more additional JAX-RS services were built beyond the simple customer database example that has been repeated so many times throughout this book. Chapter 2 discussed the design of an e-commerce application. This chapter starts the process of implementing this application by introducing an order-entry RESTful service.

The Server Code

To make it easier to publish Link headers, the Link class from ex09_1 is expanded to add a toString() and valueOf() method:

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

@XmlRootElement(name = "link") public class Link { ... /** * To write as link header * * @return */ public String toString() { StringBuilder builder = new StringBuilder("<"); builder.append(href).append(">; rel=").append(relationship); if (type != null) builder.append("; type=").append(type); return builder.toString(); } private static Pattern parse = Pattern.compile( "<(.+)>\\s*;\\s*(.+)"); /** * For unmarshalling Link Headers. * Its not an efficient or perfect algorithm * and does make a few assumptiosn * * @param val ...

The best content for your career. Discover unlimited learning on demand for around $1/day.