Cover image for RESTful Java with JAX-RS 2.0, 2nd Edition

Book description

Learn how to design and develop distributed web services in Java, using RESTful architectural principles and the JAX-RS 2.0 specification in Java EE 7. By focusing on implementation rather than theory, this hands-on reference demonstrates how easy it is to get started with services based on the REST architecture.

Table of Contents

  1. RESTful Java with JAX-RS 2.0
  2. Foreword
  3. Preface
    1. Author’s Note
    2. Who Should Read This Book
    3. How This Book Is Organized
      1. Part I, REST and the JAX-RS Standard
      2. Part II, JAX-RS Workbook
    4. Conventions Used in This Book
    5. Using Code Examples
    6. Safari® Books Online
    7. How to Contact Us
    8. Acknowledgments
  4. I. REST and the JAX-RS Standard
    1. 1. Introduction to REST
      1. REST and the Rebirth of HTTP
      2. RESTful Architectural Principles
        1. Addressability
        2. The Uniform, Constrained Interface
        3. Why Is the Uniform Interface Important?
        4. Representation-Oriented
        5. Communicate Statelessly
        6. HATEOAS
          1. The engine of application state
      3. Wrapping Up
    2. 2. Designing RESTful Services
      1. The Object Model
      2. Model the URIs
      3. Defining the Data Format
        1. Read and Update Format
          1. Common link element
          2. The details
        2. Create Format
      4. Assigning HTTP Methods
        1. Browsing All Orders, Customers, or Products
        2. Obtaining Individual Orders, Customers, or Products
        3. Creating an Order, Customer, or Product
          1. Creating with PUT
          2. Creating with POST
        4. Updating an Order, Customer, or Product
        5. Removing an Order, Customer, or Product
        6. Cancelling an Order
          1. Overloading the meaning of DELETE
          2. States versus operations
      5. Wrapping Up
    3. 3. Your First JAX-RS Service
      1. Developing a JAX-RS RESTful Service
        1. Customer: The Data Class
        2. CustomerResource: Our JAX-RS Service
          1. Creating customers
          2. Retrieving customers
          3. Updating a customer
          4. Utility methods
        3. JAX-RS and Java Interfaces
        4. Inheritance
      2. Deploying Our Service
      3. Writing a Client
      4. Wrapping Up
    4. 4. HTTP Method and URI Matching
      1. Binding HTTP Methods
        1. HTTP Method Extensions
      2. @Path
        1. Binding URIs
        2. @Path Expressions
          1. Template parameters
          2. Regular expressions
          3. Precedence rules
          4. Encoding
        3. Matrix Parameters
      3. Subresource Locators
        1. Full Dynamic Dispatching
      4. Gotchas in Request Matching
      5. Wrapping Up
    5. 5. JAX-RS Injection
      1. The Basics
      2. @PathParam
        1. More Than One Path Parameter
        2. Scope of Path Parameters
        3. PathSegment and Matrix Parameters
          1. Matching with multiple PathSegments
        4. Programmatic URI Information
      3. @MatrixParam
      4. @QueryParam
        1. Programmatic Query Parameter Information
      5. @FormParam
      6. @HeaderParam
        1. Raw Headers
      7. @CookieParam
      8. @BeanParam
      9. Common Functionality
        1. Automatic Java Type Conversion
          1. Primitive type conversion
          2. Java object conversion
          3. ParamConverters
          4. Collections
          5. Conversion failures
        2. @DefaultValue
        3. @Encoded
      10. Wrapping Up
    6. 6. JAX-RS Content Handlers
      1. Built-in Content Marshalling
        1. javax.ws.rs.core.StreamingOutput
        2. java.io.InputStream, java.io.Reader
        3. java.io.File
        4. byte[]
        5. String, char[]
        6. MultivaluedMap<String, String> and Form Input
        7. javax.xml.transform.Source
      2. JAXB
        1. Intro to JAXB
        2. JAXB JAX-RS Handlers
          1. Managing your own JAXBContexts with ContextResolvers
        3. JAXB and JSON
          1. XML to JSON using BadgerFish
        4. JSON and JSON Schema
      3. Custom Marshalling
        1. MessageBodyWriter
          1. Adding pretty printing
          2. Pluggable JAXBContexts using ContextResolvers
        2. MessageBodyReader
        3. Life Cycle and Environment
      4. Wrapping Up
    7. 7. Server Responses and Exception Handling
      1. Default Response Codes
        1. Successful Responses
        2. Error Responses
      2. Complex Responses
        1. Returning Cookies
        2. The Status Enum
        3. javax.ws.rs.core.GenericEntity
      3. Exception Handling
        1. javax.ws.rs.WebApplicationException
        2. Exception Mapping
        3. Exception Hierarchy
          1. Mapping default exceptions
      4. Wrapping Up
    8. 8. JAX-RS Client API
      1. Client Introduction
      2. Bootstrapping with ClientBuilder
      3. Client and WebTarget
      4. Building and Invoking Requests
        1. Invocation
        2. Exception Handling
      5. Configuration Scopes
      6. Wrapping Up
    9. 9. HTTP Content Negotiation
      1. Conneg Explained
        1. Preference Ordering
      2. Language Negotiation
      3. Encoding Negotiation
      4. JAX-RS and Conneg
        1. Method Dispatching
        2. Leveraging Conneg with JAXB
        3. Complex Negotiation
          1. Viewing Accept headers
          2. Variant processing
        4. Negotiation by URI Patterns
      5. Leveraging Content Negotiation
        1. Creating New Media Types
        2. Flexible Schemas
      6. Wrapping Up
    10. 10. HATEOAS
      1. HATEOAS and Web Services
        1. Atom Links
        2. Advantages of Using HATEOAS with Web Services
          1. Location transparency
          2. Decoupling interaction details
          3. Reduced state transition errors
          4. W3C standardized relationships
        3. Link Headers Versus Atom Links
      2. HATEOAS and JAX-RS
        1. Building URIs with UriBuilder
        2. Relative URIs with UriInfo
      3. Building Links and Link Headers
        1. Writing Link Headers
        2. Embedding Links in XML
      4. Wrapping Up
    11. 11. Scaling JAX-RS Applications
      1. Caching
        1. HTTP Caching
        2. Expires Header
        3. Cache-Control
        4. Revalidation and Conditional GETs
          1. Last-Modified
          2. ETag
          3. JAX-RS and conditional GETs
      2. Concurrency
        1. JAX-RS and Conditional Updates
      3. Wrapping Up
    12. 12. Filters and Interceptors
      1. Server-Side Filters
        1. Server Request Filters
        2. Server Response Filters
      2. Reader and Writer Interceptors
      3. Client-Side Filters
      4. Deploying Filters and Interceptors
      5. Ordering Filters and Interceptors
      6. Per-JAX-RS Method Bindings
        1. DynamicFeature
        2. Name Bindings
        3. DynamicFeature Versus @NameBinding
      7. Exception Processing
      8. Wrapping Up
    13. 13. Asynchronous JAX-RS
      1. AsyncInvoker Client API
        1. Using Futures
          1. Exception handling
        2. Using Callbacks
        3. Futures Versus Callbacks
      2. Server Asynchronous Response Processing
        1. AsyncResponse API
        2. Exception Handling
        3. Cancel
        4. Status Methods
        5. Timeouts
        6. Callbacks
        7. Use Cases for AsyncResponse
          1. Server-side push
          2. Publish and subscribe
          3. Priority scheduling
      3. Wrapping Up
    14. 14. Deployment and Integration
      1. Deployment
        1. The Application Class
        2. Deployment Within a JAX-RS-Aware Container
        3. Deployment Within a JAX-RS-Unaware Container
      2. Configuration
        1. Basic Configuration
      3. EJB Integration
      4. Spring Integration
      5. Wrapping Up
    15. 15. Securing JAX-RS
      1. Authentication
        1. Basic Authentication
        2. Digest Authentication
        3. Client Certificate Authentication
      2. Authorization
      3. Authentication and Authorization in JAX-RS
        1. Enforcing Encryption
        2. Authorization Annotations
      4. Programmatic Security
      5. Client Security
        1. Verifying the Server
      6. OAuth 2.0
      7. Signing and Encrypting Message Bodies
        1. Digital Signatures
          1. DKIM/DOSETA
          2. JOSE JWS
        2. Encrypting Representations
      8. Wrapping Up
    16. 16. Alternative Java Clients
      1. java.net.URL
        1. Caching
        2. Authentication
          1. Client Certificate Authentication
        3. Advantages and Disadvantages
      2. Apache HttpClient
        1. Authentication
          1. Client Certificate authentication
        2. Advantages and Disadvantages
      3. RESTEasy Client Proxies
        1. Advantages and Disadvantages
      4. Wrapping Up
  5. II. JAX-RS Workbook
    1. 17. Workbook Introduction
      1. Installing RESTEasy and the Examples
      2. Example Requirements and Structure
        1. Code Directory Structure
        2. Environment Setup
    2. 18. Examples for Chapter 3
      1. Build and Run the Example Program
        1. Deconstructing pom.xml
        2. Running the Build
      2. Examining the Source Code
    3. 19. Examples for Chapter 4
      1. Example ex04_1: HTTP Method Extension
        1. Build and Run the Example Program
        2. The Server Code
        3. The Client Code
      2. Example ex04_2: @Path with Expressions
        1. Build and Run the Example Program
        2. The Server Code
        3. The Client Code
      3. Example ex04_3: Subresource Locators
        1. Build and Run the Example Program
        2. The Server Code
        3. The Client Code
    4. 20. Examples for Chapter 5
      1. Example ex05_1: Injecting URI Information
        1. The Server Code
        2. The Client Code
        3. Build and Run the Example Program
      2. Example ex05_2: Forms and Cookies
        1. The Server Code
        2. Build and Run the Example Program
    5. 21. Examples for Chapter 6
      1. Example ex06_1: Using JAXB
        1. The Client Code
        2. Changes to pom.xml
        3. Build and Run the Example Program
      2. Example ex06_2: Creating a Content Handler
        1. The Content Handler Code
        2. The Resource Class
        3. The Application Class
        4. The Client Code
        5. Build and Run the Example Program
    6. 22. Examples for Chapter 7
      1. Example ex07_1: ExceptionMapper
        1. The Client Code
        2. Build and Run the Example Program
    7. 23. Examples for Chapter 9
      1. Example ex09_1: Conneg with JAX-RS
        1. The Client Code
        2. Build and Run the Example Program
      2. Example ex09_2: Conneg via URL Patterns
        1. The Server Code
        2. Build and Run the Example Program
    8. 24. Examples for Chapter 10
      1. Example ex10_1: Atom Links
        1. The Server Code
        2. The Client Code
        3. Build and Run the Example Program
      2. Example ex10_2: Link Headers
        1. The Server Code
          1. OrderResource
          2. StoreResource
        2. The Client Code
        3. Build and Run the Example Program
    9. 25. Examples for Chapter 11
      1. Example ex11_1: Caching and Concurrent Updates
        1. The Server Code
        2. The Client Code
        3. Build and Run the Example Program
    10. 26. Examples for Chapter 12
      1. Example ex12_1 : ContainerResponseFilter and DynamicFeature
        1. The Server Code
        2. The Client Code
        3. Build and Run the Example Program
      2. Example ex12_2: Implementing a WriterInterceptor
        1. The Client Code
        2. Build and Run the Example Program
    11. 27. Examples for Chapter 13
      1. Example ex13_1: Chat REST Interface
        1. The Client Code
        2. The Server Code
          1. Posting a new message
          2. Handling poll requests
        3. Build and Run the Example Program
    12. 28. Examples for Chapter 14
      1. Example ex14_1: EJB and JAX-RS
        1. Project Structure
        2. The EJBs
        3. The Remaining Server Code
          1. The ExceptionMappers
          2. Changes to Application class
        4. The Client Code
        5. Build and Run the Example Program
      2. Example ex14_2: Spring and JAX-RS
        1. Build and Run the Example Program
    13. 29. Examples for Chapter 15
      1. Example ex15_1: Custom Security
        1. One-Time Password Authentication
          1. The server code
        2. Allowed-per-Day Access Policy
          1. The client code
        3. Build and Run the Example Program
      2. Example ex15_1: JSON Web Encryption
        1. Build and Run the Example Program
  6. Index
  7. About the Author
  8. Colophon
  9. Copyright