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

HATEOAS and JAX-RS

JAX-RS doesn’t have many facilities to help with HATEOAS. HATEOAS is defined by the application, so there’s not much a framework can add. What it does have, though, are helper classes that you can use to build the URIs that you link to in your data formats.

Building URIs with UriBuilder

One such helper class is javax.ws.rs.core.UriBuilder. The UriBuilder class allows you to construct a URI piece by piece and is also sensitive to template parameters:

public abstract class UriBuilder {
   public static UriBuilder fromUri(URI uri)
                                      throws IllegalArgumentException
   public static UriBuilder fromUri(String uri)
                                      throws IllegalArgumentException
   public static UriBuilder fromPath(String path)
                                      throws IllegalArgumentException
   public static UriBuilder fromResource(Class<?> resource)
                                      throws IllegalArgumentException

UriBuilder instances can only be instantiated from the static helper methods listed. They can be initialized by a URI, path, or the @Path annotation of a JAX-RS resource class:

 public abstract UriBuilder clone(); public abstract UriBuilder uri(URI uri) throws IllegalArgumentException; public abstract UriBuilder scheme(String scheme) throws IllegalArgumentException; public abstract UriBuilder schemeSpecificPart(String ssp) throws IllegalArgumentException; public abstract UriBuilder userInfo(String ui); public abstract UriBuilder host(String host) throws IllegalArgumentException; public abstract UriBuilder port(int port) throws IllegalArgumentException; public abstract UriBuilder ...

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