Modern API Development with Spring 6 and Spring Boot 3: Design scalable, viable, and reactive APIs with REST, gRPC, and GraphQL

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

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 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 into 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.

Author(s): SOURABH SHARMA
Edition: 2
Publisher: Packt Publishing
Year: 2023

Language: English
Pages: 494

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