O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Building a RESTful Web Service with Spring

Book Description

A hands-on guide to building an enterprise-grade, scalable RESTful web service using the Spring Framework

About This Book

  • Follow best practices and explore techniques such as clustering and caching to achieve a scalable web service
  • Leverage the Spring Framework to quickly implement RESTful endpoints
  • Learn to implement a client library for a RESTful web service using the Spring Framework

Who This Book Is For

This book is intended for those who want to learn to build RESTful web services with the Spring Framework. To make best use of the code samples included in the book, you should have a basic knowledge of the Java language. Previous experience with the Spring Framework would also help you get up and running quickly.

What You Will Learn

  • Deep dive into the principles behind REST
  • Expose CRUD operations through RESTful endpoints with the Spring Framework
  • Devise response formats and error handling strategies, offering a consistent and flexible structure to simplify integration for service consumers
  • Follow the best approaches for dealing with a service’s evolution while maintaining backward compatibility
  • Understand techniques to secure web services
  • Comply with the best ways to test RESTful web services, including tips for load testing
  • Optimise and scale web services using techniques such as caching and clustering

In Detail

REST is an architectural style that tackles the challenges of building scalable web services. In today’s connected world, APIs have taken a central role on the web. APIs provide the fabric through which systems interact, and REST has become synonymous with APIs.

The depth, breadth, and ease of use of Spring makes it one of the most attractive frameworks in the Java ecosystem. Marrying the two technologies is therefore a very natural choice.

This book takes you through the design of RESTful web services and leverages the Spring Framework to implement these services. Starting from the basics of the philosophy behind REST, you’ll go through the steps of designing and implementing an enterprise-grade RESTful web service. Taking a practical approach, each chapter provides code samples that you can apply to your own circumstances.

This book goes beyond the use of Spring and explores approaches to tackle resilience, security, and scalability concerns. You’ll learn techniques to deal with security in Spring and discover how to implement unit and integration test strategies.

Finally, the book ends by walking you through building a Java client for your RESTful web service, along with some scaling techniques for it.

Style and approach

This book is a step-by-step, hands-on guide to designing and building RESTful web services. The book follows the natural cycle of developing these services and includes multiple code samples to help you.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Building a RESTful Web Service with Spring
    1. Table of Contents
    2. Building a RESTful Web Service with Spring
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. 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
    8. 1. A Few Basics
      1. REST principles
        1. Uniform interface
        2. Client-Server
        3. Stateless
        4. Cacheable
        5. Layered system
        6. Code on demand
      2. The Spring Framework and REST
      3. Our RESTful web service
        1. Architecture
        2. Data model
      4. Summary
    9. 2. Building RESTful Web Services with Maven and Gradle
      1. Apache Maven
        1. Dependency management in Apache Maven
      2. Gradle
        1. Dependency management in Gradle
      3. The structure of our sample web service
        1. The anatomy of a Service Module
          1. Local versus Remote Service Invocations
      4. Developing RESTful web services
        1. Working with your favorite IDE
        2. Making services executable
        3. Starting services with Maven
      5. Summary
    10. 3. The First Endpoint
      1. The Inventory service
      2. REST and the MVC pattern
        1. Request mapping
        2. Path mapping
        3. HTTP method mapping
        4. Request parameter mapping
      3. Running the service
      4. A few words on data representation
      5. Summary
    11. 4. Data Representation
      1. The Data-Transfer-Object design pattern
      2. The API response format
        1. The envelope format
          1. Error management
          2. Pagination support
        2. Customizing JSON responses
      3. API evolutions
        1. HATEOAS
        2. Versioning strategies
          1. URI versioning
          2. Representation versioning
          3. Other approaches
      4. Summary
    12. 5. CRUD Operations in REST
      1. Mapping CRUD operations to HTTP methods
      2. Creating resources
        1. Quickly testing endpoints
        2. JSON versus form data
      3. Updating resources
        1. The update endpoint
        2. Testing update requests
      4. Deleting resources
      5. Overriding the HTTP method
      6. Summary
    13. 6. Performance
      1. HTTP compression
        1. Content negotiation
        2. gzip or deflate?
        3. gzip compression in RESTful web services
          1. Spring Boot
      2. HTTP caching
        1. Cache-Control
          1. Private/Public caching
          2. No caching
          3. ETags
          4. Last-Modified/If-Modified-Since headers
      3. Room availability
        1. An overview of implementation
        2. The REST resource
        3. Adding HTTP caching
        4. Caching with ETags
      4. Summary
    14. 7. Dealing with Security
      1. The booking service
        1. The REST resource
      2. Authentication
        1. HTTP Basic authentication
          1. Using Basic authentication with Spring
        2. HTTP Digest authentication
        3. Token-based authentication
        4. Other authentication methods
      3. Authorization
        1. Authorization with Spring
          1. URL mapping
          2. Resource annotations
      4. Input validation
        1. Java Bean annotations
          1. Regular expressions
          2. Validating bookings
      5. Encryption
        1. Storing sensitive data
      6. Summary
    15. 8. Testing RESTful Web Services
      1. Unit testing Spring controllers
      2. Mocking
        1. Simple mocking
        2. Implementation stubbing with a mocking library
          1. Mockito and Spring
      3. Testing security
      4. Integration testing
        1. Continuous delivery
        2. Integration tests with Spring Boot
        3. Postman
          1. Postman and security
      5. Other forms of testing
        1. User Acceptance Testing
        2. Load testing
      6. Summary
    16. 9. Building a REST Client
      1. The basic setup
        1. Declaring a client
      2. Remote versus local clients
        1. Availability and booking services
      3. Handling security
        1. The Basic authentication
        2. The Digest authentication
        3. HTTP public key pinning
      4. Exception handling
      5. Summary
    17. 10. Scaling a RESTful Web Service
      1. Clustering
        1. Scaling up versus scaling out
        2. The benefits of clustered systems
        3. Load balancing
        4. Linear scalability
      2. Distributed caching
        1. Data-tier caching
          1. First-level caching
          2. Second-level caching
        2. Application-tier caching
      3. Asynchronous communication
      4. Summary
    18. Index