Modern API Development with Spring 6 and Spring Boot 3: Design scalable, viable, and reactive APIs with REST, gRPC, and GraphQL [2 ed.] 9781804613276

Discover ways to enhance your application's functionality through hands-on learning for designing, testing, securin

1,182 581 7MB

English Pages 494 Year 2023

Report DMCA / Copyright

DOWNLOAD FILE

Modern API Development with Spring 6 and Spring Boot 3: Design scalable, viable, and reactive APIs with REST, gRPC, and GraphQL [2 ed.]
 9781804613276

Table of contents :
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

Polecaj historie