RESTful Java Web Services - Third Edition

Book description

Master core REST concepts and create RESTful web services in Java

About This Book

  • Build efficient and secure RESTful web APIs in Java..
  • Design solutions to produce, consume and visualize RESTful web services using WADL, RAML, and Swagger
  • Familiarize the role of RESTful APIs usage in emerging technology trends like Cloud, IoT, Social Media.

Who This Book Is For

If you are a web developer with a basic understanding of the REST concepts and envisage to get acquainted with the idea of designing and developing RESTful web services, this is the book for you. As all the code samples for the book are written in Java, proficiency in Java is a must.

What You Will Learn

  • Introduce yourself to the RESTful software architectural style and the REST API design principles
  • Make use of the JSR 353 API, JSR 374 API, JSR 367 API and Jackson API for JSON processing
  • Build portable RESTful web APIs, making use of the JAX-RS 2.1 API
  • Simplify API development using the Jersey and RESTEasy extension APIs
  • Secure your RESTful web services with various authentication and authorization mechanisms
  • Get to grips with the various metadata solutions to describe, produce, and consume RESTful web services
  • Understand the design and coding guidelines to build well-performing RESTful APIs
  • See how the role of RESTful web services changes with emerging technologies and trends

In Detail

Representational State Transfer (REST) is a simple yet powerful software architecture style to create lightweight and scalable web services. The RESTful web services use HTTP as the transport protocol and can use any message formats, including XML, JSON(widely used), CSV, and many more, which makes it easily inter-operable across different languages and platforms.

This successful book is currently in its 3rd edition and has been used by thousands of developers. It serves as an excellent guide for developing RESTful web services in Java.

This book attempts to familiarize the reader with the concepts of REST. It is a pragmatic guide for designing and developing web services using Java APIs for real-life use cases following best practices and for learning to secure REST APIs using OAuth and JWT. Finally, you will learn the role of RESTful web services for future technological advances, be it cloud, IoT or social media.

By the end of this book, you will be able to efficiently build robust, scalable, and secure RESTful web services using Java APIs.

Style and approach

Step-by-step guide to designing and developing robust RESTful web services. Each topic is explained in a simple and easy-to-understand manner with lots of real-life use-cases and their solutions.

Publisher resources

Download Example Code

Table of contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. Introducing the REST Architectural Style
    1. The REST architectural style
    2. Introducing HTTP
      1. HTTP versions
      2. Understanding the HTTP request-response model
      3. Uniform resource identifier
      4. Understating the HTTP request methods
      5. Representing content types using HTTP header fields
      6. HTTP status codes
    3. The evolution of RESTful web services
    4. The core architectural elements of a RESTful system
      1. Data elements
      2. Resources
      3. URI
      4. The representation of resources
      5. Generic interaction semantics for REST resources
        1. The HTTP GET method
        2. The HTTP POST method
        3. The HTTP PUT method
        4. The HTTP DELETE method
      6. Hypermedia as the Engine of Application State
      7. Connectors
      8. Components
    5. The description and discovery of RESTful web services
    6. Java tools and frameworks for building RESTful web services
    7. Summary
  3. Java APIs for JSON Processing
    1. A brief overview of JSON
      1. Understanding the JSON data syntax
      2. Basic data types available with JSON
      3. Sample JSON file representing employee objects
    2. Processing JSON data
    3. Using JSR 353 – Java API for processing JSON
      1. Processing JSON with JSR 353 object model APIs
        1. Generating the object model from the JSON representation
          1. JSON value types
        2. Generating the JSON representation from the object model
      2. Processing JSON with JSR 353 streaming APIs
        1. Using streaming APIs to parse JSON data
        2. Using streaming APIs to generate JSON
    4. Using the Jackson API for processing JSON
      1. Processing JSON with Jackson tree model APIs
        1. Using Jackson tree model APIs to query and update data
      2. Processing JSON with Jackson data binding APIs
        1. Simple Jackson data binding with generalized objects
        2. Full Jackson data binding with specialized objects
      3. Processing JSON with Jackson streaming APIs
        1. Using Jackson streaming APIs to parse JSON data
        2. Using Jackson streaming APIs to generate JSON
    5. Using the Gson API for processing JSON
      1. Processing JSON with object model APIs in Gson
        1. Generating the object model from the JSON representation
        2. Generating the parameterized Java collection from the JSON representation
        3. Generating the JSON representation from the object model
      2. Processing JSON with Gson streaming APIs
        1. Reading JSON data with Gson streaming APIs
        2. Writing JSON data with Gson streaming APIs
    6. Java EE 8 enhancements for processing JSON
      1. Using the JSR 374 – Java API for JSON Processing 1.1
        1. Understanding the JSON Pointer
          1. Processing JSON using JSON Pointer
        2. Understanding the JSON Patch
          1. Processing JSON using JSON Patch
      2. Using the JSR 367 – Java API for JSON Binding
        1. Processing JSON using JSON-B
    7. Summary
  4. Introducing the JAX-RS API
    1. An overview of JAX-RS
    2. JAX-RS annotations
      1. Specifying the dependency of the JAX-RS API
      2. Using JAX-RS annotations to build RESTful web services
        1. Annotations for defining a RESTful resource
          1. @Path
        2. Annotations for specifying request-response media types
          1. @Produces
          2. @Consumes
        3. Annotations for processing HTTP request methods
          1. @GET
          2. @PUT
          3. @POST
          4. @DELETE
          5. @HEAD
          6. @OPTIONS
        4. Annotations for accessing request parameters
          1. @PathParam
          2. @QueryParam
          3. @MatrixParam
          4. @HeaderParam
          5. @CookieParam
          6. @FormParam
          7. @DefaultValue
          8. @Context
          9. @BeanParam
          10. @Encoded
        5. Annotation inheritance
    3. Returning additional metadata with responses
    4. Understanding data binding rules in JAX-RS
      1. Mapping the path variable with Java types
      2. Mapping the request and response entity body with Java types
        1. Using JAXB to manage the mapping of the request and response entity body to Java objects
    5. Building your first RESTful web service with JAX-RS
      1. Setting up the environment
      2. Building a simple RESTful web service application using the NetBeans IDE
      3. Adding CRUD operations on the REST resource class
    6. Client APIs for accessing RESTful web services
      1. Specifying a dependency of the JAX-RS client API
      2. Calling REST APIs using the JAX-RS client
        1. Simplified client APIs for accessing REST APIs
    7. Summary
  5. Advanced Features in the JAX-RS APIs
    1. Understanding subresources and subresource locators in JAX-RS
      1. Subresources in JAX-RS
      2. Subresource locators in JAX-RS
      3. Dynamic dispatching
      4. Request matching
    2. JAX-RS response builder explained
    3. Exception handling in JAX-RS
      1. Reporting errors using ResponseBuilder
      2. Reporting errors using WebApplicationException
    4. Reporting errors using application exceptions
      1. Mapping exceptions to a response message using ExceptionMapper
    5. Introducing validations in JAX-RS applications
      1. A brief introduction to Bean Validation
      2. Building custom validation constraints
      3. What happens when Bean Validation fails in a JAX-RS application?
    6. Supporting custom request-response message formats
      1. Building custom entity provider
        1. Marshaling Java objects to the CSV representation with MessageBodyWriter
        2. Marshaling CSV representation to Java objects with MessageBodyReader
    7. Asynchronous RESTful web services
    8. Asynchronous RESTful web service client
    9. Server-sent events
    10. Managing an HTTP cache in a RESTful web service
      1. Using the Expires header to control the validity of the HTTP cache
      2. Using Cache-Control directives to manage the HTTP cache
      3. Conditional request processing with the Last-Modified HTTP response header
      4. Conditional request processing with the ETag HTTP response header
      5. Conditional data update in RESTful web services
    11. Understanding filters and interceptors in JAX-RS
      1. Modifying request and response parameters with JAX-RS filters
        1. Implementing server-side request message filters
          1. Postmatching server-side request message filters
          2. Prematching server-side request message filters
        2. Implementing server-side response message filters
        3. Implementing client-side request message filters
        4. Implementing client-side response message filters
      2. Modifying request and response message bodies with JAX-RS interceptors
        1. Implementing request message body interceptors
        2. Implementing response message body interceptors
      3. Managing the order of execution for filters and interceptors
      4. Selectively applying filters and interceptors on REST resources by using @NameBinding
      5. Dynamically applying filters and interceptors on REST resources using DynamicFeature
    12. Understanding the JAX-RS resource life cycle
    13. Summary
  6. Introducing JAX-RS Implementation Framework Extensions
    1. Jersey framework extensions
      1. Dynamically configuring JAX-RS resources during deployment
      2. A quick look at the static resource configurations
      3. Modifying JAX-RS resources during deployment using ModelProcessor
      4. What is the Jersey model processor and how it works?
        1. A brief look at the ModelProcessor interface
      5. Building Hypermedia As The Engine Of Application State (HATEOAS) APIs
      6. Programmatically building entity body links using JAX-RS APIs
      7. Programmatically building header links using JAX-RS APIs
      8. Declaratively building links using Jersey annotations
        1. Specifying the dependency to use Jersey declarative linking
        2. Enabling the Jersey declarative linking feature for the application
        3. Declaratively adding links to the resource representation
        4. Grouping multiple links using @InjectLinks
        5. Declaratively building HTTP link headers using @InjectLinks
      9. Reading and writing large binary objects using Jersey APIs
        1. Building RESTful web services for storing images
        2. Building RESTful web service for reading images
    2. Generating a chunked output using Jersey APIs
      1. Jersey client API for reading chunked input
      2. Supporting server-sent events in RESTful web services
      3. Understanding the Jersey server-side configuration properties
      4. Monitoring RESTful web services using Jersey APIs
    3. RESTEasy framework extensions
      1. Caching using RESTEasy
        1. Cache-control annotations
        2. Client-side caching
      2. GZIP compression/decompression
      3. Multipart content handling
    4. Summary
  7. Securing RESTful Web Services
    1. Securing and authenticating web services
    2. HTTP basic authentication
      1. Building JAX-RS clients with basic authentication
      2. Securing JAX-RS services with basic authentication
        1. Configuring the basic authentication
          1. Defining groups and users in the GlassFish server
    3. HTTP digest authentication
    4. JWT authentication
      1. JSON Web Token (JWT) overview
      2. Using JWT to secure RESTful services
    5. Securing RESTful web services with OAuth
      1. Understanding the OAuth 1.0 protocol
        1. Building the OAuth 1.0 client using Jersey APIs
      2. Understanding the OAuth 2.0 protocol
        1. Understanding the grant types in OAuth 2.0
        2. Building the OAuth 2.0 client using Jersey APIs
    6. Authorizing the RESTful web service accesses via the security APIs
      1. Using SecurityContext APIs to control access
      2. Using the javax.annotation.security annotations to control access
      3. Using Jersey's role-based entity data filtering
    7. Input validation
    8. Key considerations for securing RESTful services
    9. Summary
  8. Description and Discovery of RESTful Web Services
    1. The need for an interface contract
    2. Web Application Description Language
      1. Overview of the WADL structure
      2. Generating WADL from JAX-RS
      3. Generating a Java client from WADL
      4. Market adoption of WADL
    3. RESTful API Modeling Language
      1. Overview of the RAML structure
      2. Generating RAML from JAX-RS
        1. Generating RAML from JAX-RS via CLI
      3. Generating JAX-RS from RAML
        1. Generating JAX-RS from RAML via CLI
      4. A glance at the market adoption of RAML
    4. Swagger
      1. A quick overview of the Swagger structure
      2. An overview of Swagger APIs
      3. Generating Swagger from JAX-RS
        1. Specifying dependency to Swagger
        2. Configuring the Swagger definition
        3. Adding a Swagger annotation on a JAX-RS resource class
      4. Generating a Java client from Swagger
      5. A glance at the market adoption of Swagger
    5. Revisiting the features offered in WADL, RAML, and Swagger
    6. Summary
  9. RESTful API Design Guidelines
    1. Designing RESTful web APIs
      1. Identifying resources in a problem domain
      2. Transforming operations to HTTP methods
        1. Understanding the difference between PUT and POST
      3. Naming RESTful web resources
      4. Using HATEOAS in response representation
        1. Hypertext Application Language
        2. RFC 5988 - web linking
    2. Fine-grained and coarse-grained resource APIs
    3. Using header parameters for content negotiation
    4. Multilingual RESTful web API resources
    5. Representing date and time in RESTful web resources
    6. Implementing partial response
    7. Implementing partial update
    8. Returning modified resources to the caller
    9. Paging a resource collection
    10. Implementing search and sort operations
    11. Versioning RESTful web APIs
      1. Including the version in the resource URI –  URI versioning
      2. Including the version in a custom HTTP request header – HTTP header versioning
      3. Including the version in the HTTP Accept header – media type versioning
      4. Hybrid approach for versioning APIs
    12. Caching RESTful web API results
      1. HTTP Cache-Control directive
      2. HTTP conditional requests
    13. Using HTTP status codes in RESTful web APIs
    14. Overriding HTTP methods
    15. Documenting RESTful web APIs
    16. Asynchronous execution of RESTful web APIs
    17. Microservice architecture style for RESTful web application
    18. A quick recap
    19. Summary
  10. The Role of RESTful APIs in Emerging Technologies
    1. Cloud services
      1. Cloud characteristics
      2. Cloud offering models
      3. RESTful API Role in cloud services
        1. Provisioning IT resources using RESTful APIs
          1. Locating the REST API endpoint
          2. Generating an authentication cookie
          3. Provisioning a virtual machine instance
    2. Internet of things
      1. IoT platform
      2. IoT benefits
      3. RESTful API role in the IoT
    3. Modern web applications
      1. Single-page applications
        1. RESTFul API role in single-page applications
    4. Social media
      1. Social media platforms
      2. Social media benefits
      3. RESTful API role in social media
    5. Using Open Data Protocol with RESTful web APIs
      1. A quick look at OData
        1. URI convention for OData-based REST APIs
        2. Reading resources
        3. Querying data
        4. Modifying data
        5. Relationship operations
    6. Summary
  11. Useful Features and Techniques
    1. Tools for building a JAX-RS application
    2. Integration testing of JAX-RS resources with Arquillian
      1. Adding Arquillian dependencies to the Maven-based project
      2. Configuring the container for running the tests
      3. Adding Arquillian test classes to the project
      4. Running Arquillian tests
    3. Using third-party entity provider frameworks with Jersey
      1. Transforming the JPA model into OData-enabled RESTful web services
    4. Packaging and deploying JAX-RS applications
      1. Packaging JAX-RS applications with an Application subclass
      2. Packaging the JAX-RS applications with web.xml and an Application subclass
        1. Configuring web.xml for a servlet 2.x container
        2. Configuring web.xml for a servlet 3.x container
      3. Packaging the JAX-RS applications with web.xml and without an Application subclass
        1. Configuring web.xml for the servlet 2.x container
        2. Configuring web.xml for the servlet 3.x container
    5. Summary

Product information

  • Title: RESTful Java Web Services - Third Edition
  • Author(s): Bogunuva Mohanram Balachandar
  • Release date: November 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781788294041