Building RESTful Web Services with Spring 5 - Second Edition

Book description

Find out how to implement the REST architecture to build resilient software in Java with the help of the Spring 5.0 framework.

About This Book

  • Follow best practices and explore techniques such as clustering and caching to achieve a reactive, 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 along with the new front end framework.

Who This Book Is For

This book is intended for those who want to learn to build RESTful web services with the latest Spring 5.0 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 second edition brings forth the power of the latest Spring 5.0 release, working with MVC built-in as well as the front end framework. It then goes beyond the use of Spring to explores approaches to tackle resilience, security, and scalability concerns. Improve performance of your applications with the new HTTP 2.0 standards. 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 using the new Spring Reactive libraries.

Style and approach

Readers will be taken through a set of specific patterns and tested practices to build effective RESTful systems in Java using the Spring framework.

Table of contents

  1. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  2. A Few Basics
    1. REST – a basic understanding
      1. Uniform interface
      2. Client and server
      3. Stateless
      4. Cacheable
      5. Layered system
      6. Code on demand (COD)
      7. More on REST
    2. Imperative and Reactive programming
      1. Reactive Streams
      2. Benefits of Reactive programming
    3. Reactive programming in Java and Spring 5
    4. Our RESTful web service architecture
    5. Summary
  3. Building RESTful Web Services in Spring 5 with Maven
    1. Apache Maven
      1. Creating a project with Maven
        1. Viewing a POM file after creating a project
      2. POM file structure
      3. Understanding POM dependencies
      4. Adding Log4j 2.9.1 to POM dependency
        1. Dependency trees
        2. Spring Boot
    2. Developing RESTful web services
      1. Creating a project base
      2. Working with your favorite IDE
    3. Summary
  4. Flux and Mono (Reactor Support) in Spring
    1. Benefits of Reactive programming
      1. Reactive Core and Streams
        1. Back pressures and Reactive Streams
        2. WebFlux
        3. Basic REST API
        4. Flux
        5. Mono
    2. User class with Reactive – REST
    3. Summary
  5. CRUD Operations in Spring REST
    1. CRUD operations in Spring REST 
      1. HTTP methods
      2. Reactive server initialization
      3. Sample values in the repository
      4. getAllUsers – mapping
      5. getAllUsers – implementation in the handler and repository
      6. Testing the endpoint – getAllUsers
      7. getUser – implementation in the handler and repository
      8. Testing the endpoint – getUser
      9. createUser – implementation in the handler and repository
      10. Testing the endpoint – createUser
      11. updateUser – implementation in the handler and repository
      12. Testing the endpoint – updateUser
      13. deleteUser – implementation in the handler and repository
      14. Testing the endpoint – deleteUser
    2. Summary
  6. CRUD Operations in Plain REST (Without Reactive) and File Upload
    1. Mapping CRUD operations to HTTP methods
    2. Creating resources
    3. CRUD operation in Spring 5 (without Reactive)
      1. getAllUsers – implementation
      2. getUser – implementation
      3. createUser – implementation
      4. updateUser – implementation
      5. deleteUser – implementation
    4. File uploads – REST API
      1. Testing the file upload
    5. Summary
  7. Spring Security and JWT (JSON Web Token)
    1. Spring Security
      1. Authentication and authorization
      2. JSON Web Token (JWT)
        1. JWT dependency
      3. Creating a JWT token
      4. Generating a token
      5. Getting a subject from a JWT token
      6. Getting a subject from a token
    2. Summary
  8. Testing RESTful Web Services
    1. JUnit
    2. MockMvc
      1. Testing a single user
    3. Postman
      1. Getting all the users – Postman
      2. Adding a user – Postman
      3. Generating a JWT – Postman
      4. Getting the subject from the token
    4. SoapUI
      1. Getting all the users – SoapUI
      2. Generating JWT SoapUI
      3. Getting the subject from the token – SoapUI
    5. jsoup
      1. Getting a user – jsoup
      2. Adding a user – jsoup
      3. Running the test cases
    6. Summary
  9. Performance
    1. HTTP compression
      1. Content negotiation
        1. Accept-Encoding
        2. Content-Encoding
        3. Server-driven content negotiation
        4. Agent-driven content negotiation
    2. HTTP caching
      1. HTTP cache control
        1. Public caching
        2. Private caching
        3. No-cache
        4. Only-if-cached
      2. Cache validation
        1. ETags
        2. Last-Modified/If-Modified-Since headers
    3. Cache implementation
      1. The REST resource
      2. Caching with ETags
    4. Summary
  10. AOP and Logger Controls
    1. Aspect-oriented programming (AOP)
      1. AOP (@Before) with execution
        1. Testing AOP @Before execution
      2. AOP (@Before) with annotation
        1. Testing AOP @Before annotation
      3. Integrating AOP with JWT
    2. Logger controls
      1. SLF4J, Log4J, and Logback
        1. Logback framework
        2. Logback dependency and configuration
        3. Logging levels
        4. Logback implementation in class
    3. Summary
  11. Building a REST Client and Error Handling
    1. Building a REST client
      1. RestTemplate
    2. Error handling
      1. Customized exception
    3. Summary
  12. Scaling
    1. Clustering
      1. Benefits of clustering
    2. Load balancing
    3. Scaling databases
      1. Vertical scaling
      2. Horizontal scaling
      3. Read replicas
      4. Pool connections
      5. Use multiple masters
      6. Load balancing in DB servers
      7. Database partitioning
        1. Sharding (horizontal partitioning)
        2. Vertical partitioning
    4. Distributed caching
      1. Data-tier caching
        1. First-level caching
        2. Second-level caching
      2. Application-tier caching
        1. Memcached
        2. Redis
        3. Hazelcast
        4. Ehcache
        5. Riak
        6. Aerospike
        7. Infinispan
        8. Cache2k
      3. Other distributed caching
        1. Amazon ElastiCache
        2. Oracle distributed cache (Coherence)
    5. Summary
  13. Microservice Basics
    1. Monolithic architecture and its drawbacks
    2. Introduction to microservices
      1. Independence and autonomy
      2. Resilience and fault tolerance
      3. Automated environment
      4. Stateless
    3. Benefits of microservices
    4. Microservice components
      1. Configuration server
      2. Load balancer
      3. Service discovery
      4. Circuit breaker
      5. Edge server
    5. Microservice tools
      1. Netflix Eureka
      2. Netflix Zuul
      3. Spring Cloud Netflix
      4. Netflix Ribbon
      5. Netflix Hystrix
      6. Netflix Turbine
      7. HashiCorp Consul
      8. Eclipse MicroProfile
    6. Summary
  14. Ticket Management – Advanced CRUD
    1. Ticket management using CRUD operations
    2. Registration
      1. User types
      2. User POJO
      3. Customer registration
      4. Admin registration
      5. CSR registration
    3. Login and token management
      1. Generating a token
      2. Customer login
      3. Admin login
      4. CSR login
    4. Ticket management
      1. Ticket POJO
        1. Getting a user by token
      2. User Ticket management
        1. Ticket controller
        2. The UserTokenRequired interface
        3. The UserTokenRequiredAspect class
      3. Getting my tickets – customer
      4. Allowing a user to view their single ticket
      5. Allowing a customer to update a ticket
        1. Updating a ticket – service (TicketServiceImpl)
      6. Deleting a ticket
        1. Deleting a service – service (TicketServiceImpl)
        2. Deleting my ticket – API (ticket controller)
    5. Admin Ticket management
      1. Allowing a admin to view all tickets
        1. Getting all tickets – service (TicketServiceImpl)
        2. Getting all tickets – API (ticket controller)
        3. The AdminTokenRequired interface
        4. The AdminTokenRequiredAspect class
      2. Admin updates a ticket
        1. Updating a ticket by admin – service (TicketServiceImpl)
      3. Allowing admin to view a single ticket
      4. Allowing admin to delete tickets
        1. Deleting tickets – service (TicketServiceImpl):
        2. Deleting tickets by admin – API (ticket controller):
    6. CSR Ticket management
      1. CSR updates a ticket
        1. CSRTokenRequired AOP
        2. CSRTokenRequiredAspect
      2. CSR view all tickets
        1. Viewing all tickets by CSR – API (ticket controller)
      3. CSR view single ticket
      4. CSR delete tickets
        1. Deleting tickets – service (TicketServivceImpl)
        2. Deleting tickets by CSR – API (ticket controller)
    7. Summary
  15. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Building RESTful Web Services with Spring 5 - Second Edition
  • Author(s): Raja CSP Raman, Ludovic Dewailly
  • Release date: January 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788475891