Modern API Development with Spring 6 and Spring Boot 3 - Second Edition

Book description

Discover ways to enhance your application's functionality through hands-on learning for designing, testing, securing, deploying, and maintaining production-ready APIs

Key Features

  • Learn how to design, develop, test, and deploy modern APIs in Java
  • Explore techniques for optimizing API performance and handling errors
  • Secure your APIs with industry-standard authentication and authorization techniques
  • Get a free PDF eBook with the purchase of the print or Kindle book

Book Description

Spring is a powerful and widely adopted framework for building scalable and reliable web applications in Java, complemented by Spring Boot, a popular extension to the framework that simplifies the setup and configuration of Spring-based applications. This book is an in-depth guide to harnessing Spring 6 and Spring Boot 3 for web development, offering practical knowledge of building modern robust web APIs and services.

The book covers a wide range of topics that are essential for API development, including RESTful web service fundamentals, Spring concepts, and API specifications. It also explores asynchronous API design, security, designing user interfaces, testing APIs, and the deployment of web services. In addition to its comprehensive coverage, this book offers a highly contextual real-world sample app that you can use as a reference for building different types of APIs for real-world applications. This sample app will lead you through the entire API development cycle, encompassing design and specification, implementation, testing, and deployment.

By the end of this book, you’ll have learned how to design, develop, test, and deploy scalable and maintainable modern APIs using Spring 6 and Spring Boot 3, along with best practices for bolstering the security and reliability of your applications and improving your application's overall functionality.

What you will learn

  • Create enterprise-level APIs using Spring and Java
  • Understand and implement REST, gRPC, GraphQL, and asynchronous APIs for various purposes
  • Develop real-world web APIs and services, from design to deployment
  • Expand your knowledge of API specifications and implementation best practices
  • Design and implement secure APIs with authorization and authentication
  • Develop microservices-based solutions with workflow and orchestration engines
  • Acquire proficiency in designing and testing user interfaces for APIs
  • Implement logging and tracing mechanisms in your services and APIs

Who this book is for

This book is for novice Java programmers, computer science graduates, coding boot camp alumni, and newcomers to the realm of creating real-world web APIs and services. It is an invaluable resource for Java developers transitioning to web development, offering an all-encompassing introduction to web service development. If you possess knowledge of fundamental programming constructs, data structures, and algorithms in Java but lack practical web development experience, this book will serve as a bridge to cultivate the essential skills for entry into the role of a web developer.

Table of contents

  1. Modern API Development with Spring 6 and Spring Boot 3
  2. Contributors
  3. About the author
  4. About the reviewers
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Conventions used
    6. Get in touch
    7. Share Your Thoughts
    8. Download a free PDF copy of this book
  6. Part 1 – RESTful Web Services
  7. Chapter 1: RESTful Web Service Fundamentals
    1. Technical requirements
    2. Introducing REST APIs
      1. The history of REST
      2. REST fundamentals
    3. Handling resources and URIs
      1. The URI syntax
      2. What is a URL?
      3. What is a URN?
    4. Exploring HTTP methods and status codes
      1. POST
      2. GET
      3. PUT
      4. DELETE
      5. PATCH
      6. HTTP status codes
    5. What is HATEOAS?
    6. Best practices for designing REST APIs
      1. Using nouns and not verbs when naming a resource in the endpoint path
      2. Using the plural form for naming the collection resource in the endpoint path
      3. Using hypermedia (HATEOAS)
      4. Versioning your APIs
      5. Nesting resources
      6. Securing APIs
      7. Maintaining documentation
      8. Complying with recommended status codes
      9. Ensuring caching
      10. Maintaining the rate limit
    7. Introducing our e-commerce app
    8. Summary
    9. Questions
    10. Answers
    11. Further reading
  8. Chapter 2: Spring Concepts and REST APIs
    1. Technical requirements
    2. Understanding the patterns and paradigms of Spring
      1. What is IoC?
      2. What is DI?
      3. What is AOP?
    3. Understanding the application of IoC containers
    4. Defining a bean and its scope
      1. The @ComponentScan annotation
      2. The bean’s scope
    5. Configuring beans using Java
      1. The @Import annotation
      2. The @DependsOn annotation
    6. How to code DI
      1. Using a constructor to define a dependency
      2. Using a setter method to define a dependency
      3. Using a class property to define a dependency
    7. Configuring a bean’s metadata using annotations
      1. How to use @Autowired?
      2. Matching by type
      3. Matching by qualifier
      4. Matching by name
      5. What is the purpose of @Primary?
      6. When can we use @Value?
    8. Writing code for AOP
    9. Why use Spring Boot?
    10. Understanding the importance of servlet dispatcher
    11. Summary
    12. Questions
    13. Answers
    14. Further reading
  9. Chapter 3: API Specifications and Implementation
    1. Technical requirements
    2. Designing APIs with OAS
    3. Understanding the basic structure of OAS
      1. The metadata sections of OAS
      2. The servers and tags sections of OAS
      3. The components section of OAS
      4. The path section of OAS
    4. Converting OAS to Spring code
    5. Implementing the OAS code interfaces
    6. Adding a Global Exception Handler
    7. Testing the implementation of the API
    8. Summary
    9. Questions
    10. Answers
    11. Further reading
  10. Chapter 4: Writing Business Logic for APIs
    1. Technical requirements
    2. Overview of the service design
    3. Adding a Repository component
      1. The @Repository annotation
      2. Configuring the database and JPA
      3. The database and seed data script
      4. Adding entities
      5. Adding repositories
    4. Adding a Service component
    5. Implementing hypermedia
    6. Enhancing the controller with a service and HATEOAS
    7. Adding ETags to API responses
    8. Testing the APIs
    9. Summary
    10. Questions
    11. Answers
    12. Further reading
  11. Chapter 5: Asynchronous API Design
    1. Technical requirements
    2. Understanding Reactive Streams
      1. Publisher
      2. Subscriber
      3. Subscription
      4. Processor
    3. Exploring Spring WebFlux
      1. Understanding reactive APIs
      2. Reactive Core
    4. Understanding DispatcherHandler
      1. Controllers
      2. Functional endpoints
    5. Implementing reactive APIs for our e-commerce app
      1. Changing OpenAPI Codegen for reactive APIs
      2. Adding Reactive dependencies to build.xml
      3. Handling exceptions
      4. Handling global exceptions for controllers
      5. Adding hypermedia links to an API response
      6. Defining an entity
      7. Adding repositories
      8. Adding services
      9. Adding controller implementations
      10. Adding H2 Console to an application
      11. Adding application configuration
      12. Testing reactive APIs
    6. Summary
    7. Questions
    8. Answers
    9. Further reading
  12. Part 2 – Security, UI, Testing, and Deployment
  13. Chapter 6: Securing REST Endpoints Using Authorization and Authentication
    1. Technical requirements
    2. Implementing authentication using Spring Security and JWT
      1. Adding the required Gradle dependencies
      2. Authentication using the OAuth 2.0 resource server
      3. Exploring the structure of JWT
    3. Securing REST APIs with JWT
      1. Adding new APIs
      2. Implementing the JWT manager
      3. Implementing new APIs
      4. Implementing the REST controllers
      5. Configuring web-based security
    4. Configuring CORS and CSRF
    5. Understanding authorization
      1. Role and authority
    6. Testing security
    7. Summary
    8. Questions
    9. Answers
    10. Further reading
  14. Chapter 7: Designing a User Interface
    1. Technical requirements
    2. Learning React fundamentals
      1. Creating a React app
      2. Exploring the basic structures and files
      3. Understanding the package.json file
      4. Bootstrapping a React app
    3. Exploring React components and other features
      1. Exploring JSX
      2. Understanding React hooks
      3. Styling components using Tailwind
    4. Designing the e-commerce app components
    5. Consuming APIs using Fetch
      1. Writing the product API client
      2. Coding the Product Listing page
      3. Why does the cart need to be fetched in ProductList?
      4. Configuring routing
      5. Developing the ProductCard component
      6. Developing the ProductDetail component
    6. Implementing authentication
      1. Creating a custom useToken hook
      2. Writing the Login component
      3. Writing the custom cart context
      4. Writing the Cart component
      5. Writing the Order component
      6. Writing the root (App) component
    7. Running the application
    8. Summary
    9. Questions
    10. Answers
    11. Further reading
  15. Chapter 8: Testing APIs
    1. Technical requirements
    2. Testing APIs and code manually
    3. Testing automation
      1. Unit testing
      2. Testing exceptions
      3. Executing unit tests
      4. Code coverage
      5. Integration testing
    4. Summary
    5. Questions
    6. Answers
    7. Further reading
  16. Chapter 9: Deployment of Web Services
    1. Technical requirements
    2. What is containerization?
    3. Building a Docker image
      1. What is Docker?
      2. Understanding Docker’s architecture
      3. Docker container life cycle
      4. Coding to build an image by adding the Actuator dependency
      5. Configuring the Spring Boot plugin task
      6. Configuring the Docker registry
      7. Executing a Gradle task to build an image
    4. Deploying an application in Kubernetes
    5. Summary
    6. Questions
    7. Answers
    8. Further reading
  17. Part 3 – gRPC, Logging, and Monitoring
  18. Chapter 10: Getting Started with gRPC
    1. Technical requirements
    2. How does gRPC work?
      1. REST versus gRPC
      2. Can I call the gRPC server from web browsers and mobile apps?
      3. Getting to know the gRPC architecture
      4. How gRPC uses Protobuf
    3. Understanding service definitions
    4. Exploring the RPC life cycle
      1. Events that impact the life cycle
      2. Understanding the gRPC server and gRPC stub
    5. Handling errors and error status codes
    6. Summary
    7. Questions
    8. Answers
    9. Further reading
  19. Chapter 11: gRPC API Development and Testing
    1. Technical requirements
    2. Writing an API
      1. Setting up the project
      2. Writing the payment gateway functionalities
    3. Developing the gRPC server
      1. Implementation of the gRPC server
      2. Implementation of the gRPC server class
      3. Testing the gRPC server
    4. Coding for handling errors
    5. Developing the gRPC client
      1. Implementing the gRPC client
      2. Testing the gRPC service
    6. Understanding microservice concepts
      1. Traditional monolithic design
      2. Monolithic design with services
      3. Microservices design
    7. Summary
    8. Questions
    9. Answers
    10. Further reading
  20. Chapter 12: Adding Logging and Tracing to Services
    1. Technical requirements
    2. Logging and tracing using the ELK stack
      1. Understanding the ELK stack
    3. Installing the ELK stack
    4. Implementing logging and tracing in the gRPC code
      1. Changing the gRPC server code
      2. Changing the gRPC client code
      3. Testing the logging and tracing changes
    5. Distributed tracing with Zipkin and Micrometer
    6. Summary
    7. Questions
    8. Answers
    9. Further reading
  21. Part 4 – GraphQL
  22. Chapter 13: Getting Started with GraphQL
    1. Technical requirements
    2. Getting to know GraphQL
      1. A brief history of GraphQL
      2. Comparing GraphQL with REST
    3. Learning the fundamentals of GraphQL
      1. Exploring the Query type
      2. Exploring the Mutation type
      3. Exploring the Subscription type
    4. Designing a GraphQL schema
      1. Understanding scalar types
      2. Understanding fragments
      3. Understanding interfaces
      4. Understanding Union types
      5. Understanding input types
      6. Designing a schema with GraphQL tools
    5. Testing GraphQL queries and mutations
    6. Solving the N+1 problem
      1. What is the N+1 problem?
      2. How can we solve the N+1 problem?
    7. Summary
    8. Questions
    9. Answers
    10. Further reading
  23. Chapter 14: GraphQL API Development and Testing
    1. Technical requirements
    2. Workflow and tooling for GraphQL
    3. Implementing the GraphQL server
      1. Creating the gRPC server project
      2. Adding the GraphQL DGS dependencies
      3. Adding the GraphQL schema
      4. Adding custom scalar types
      5. Documenting APIs
    4. Implementing GraphQL queries
    5. Writing fetchers for GraphQL queries
      1. Writing the data fetcher for product
      2. Writing the data fetcher for a collection of products
      3. Writing the field resolver using the data fetcher method
      4. Writing a data loader for solving the N+1 problem
    6. Implementing GraphQL mutations
    7. Implementing and testing GraphQL subscriptions
      1. Understanding the WebSocket sub-protocol for GraphQL
      2. Testing GraphQL subscriptions using Insomnia WebSocket
    8. Instrumenting the GraphQL APIs
      1. Adding a custom header
      2. Integration with Micrometer
    9. Test automation
      1. Testing GraphQL queries
      2. Testing GraphQL mutations
      3. Testing GraphQL subscriptions using automated test code
    10. Summary
    11. Questions
    12. Answers
    13. Further reading
  24. Index
    1. Why subscribe?
  25. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: Modern API Development with Spring 6 and Spring Boot 3 - Second Edition
  • Author(s): Sourabh Sharma
  • Release date: September 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781804613276