Reader small image

You're reading from  Modern API Development with Spring 6 and Spring Boot 3 - Second Edition

Product typeBook
Published inSep 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781804613276
Edition2nd Edition
Languages
Concepts
Right arrow
Author (1)
Sourabh Sharma
Sourabh Sharma
author image
Sourabh Sharma

Sourabh Sharma is a Senior Development Manager at Oracle with over 20 years of experience in the industry. He is a manager and architect who has been designing on-premise and cloud-based applications using Java, Javascript, and Oracle DB. Sourabh has worked with leading companies and delivered enterprise products and applications. His expertise lies in conceptualizing, modeling, designing, and developing N-tier and cloud-based web applications while leading teams. Sourabh's experience also includes developing microservice-based solutions and implementing various types of workflow and orchestration engines. He believes in continuous learning and sharing knowledge through his books and training.
Read more about Sourabh Sharma

Right arrow

Preface

This book is an in-depth guide to using Spring 6 and Spring Boot 3 for web development. Spring is a powerful and widely used framework for building scalable and reliable web applications in Java. Spring Boot is a popular extension to the framework that simplifies the setup and configuration of Spring-based applications. This book will teach you how to use these technologies to build modern and robust web APIs and services.

The book covers a wide range of topics that are essential for API development, such as fundamentals of REST/GraphQL/gRPC, Spring concepts, and API specifications and implementation. Additionally, the book covers topics such as asynchronous API design, security, designing user interfaces, testing APIs, and the deployment of web services. The book provides a highly contextual real-world sample app that readers can use as a reference for building different types of APIs for real-world apps, including the persistent database layer. The approach taken in the book is to guide readers through the entire development cycle of API development, including design and specification, implementation, testing, and deployment.

By the end of this book, you will 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 to ensure the security and reliability of your applications, and practical ideas to improve your application’s functionalities.

Who this book is for

This book is aimed at novice Java programmers, recent computer science graduates, coding boot camp alumni, and professionals who are new to creating real-world web APIs and services. It is also a valuable resource for Java developers interested in transitioning to web development and seeking a comprehensive introduction to web service development. The ideal reader possesses knowledge of fundamental programming constructs, data structures, and algorithms in Java, but lacks the practical web development experience necessary to begin working as a web developer.

What this book covers

Chapter 1, RESTful Web Service Fundamentals, drives you through the fundamentals of RESTful APIs, or, for short, REST APIs, and their design paradigm. These basics will provide you with a solid platform for developing a RESTful web service. You will also learn about the best practices while designing APIs. This chapter will also introduce the example e-commerce app that will be used across the book while learning about the different aspects of API development.

Chapter 2, Spring Concepts and REST APIs, explores the Spring fundamentals and features that are required to implement REST, gRPC, and GraphQL APIs using the Spring Framework. This will provide the technical perspective required for developing an example e-commerce app.

Chapter 3, API Specifications and Implementation, makes use of OpenAPI and Spring to implement the REST APIs. We have chosen a design-first approach to implementation. You will make use of the OpenAPI specification to first design the APIs and later implement them. You will also learn how to handle the errors that occur during the serving of the request. Here, the APIs of the example e-commerce app will be designed and implemented for reference.

Chapter 4, Writing Business Logic for APIs, helps you implement the API’s code in terms of business logic, along with data persistence in the H2 database. You will write services and repositories for implementation. You will also add hypermedia and ETag headers to API responses for optimal performance and caching.

Chapter 5, Asynchronous API Design, covers asynchronous or reactive API design, where calls will be asynchronous and non-blocking. We’ll develop these APIs using Spring WebFlux, which is itself based on Project Reactor (https://projectreactor.io). First, we’ll walk through the reactive programming fundamentals and then migrate the existing e-commerce REST APIs (the previous chapter’s code) to asynchronous (reactive) APIs to make things easier by correlating and comparing the existing (imperative) way and reactive way of programming.

Chapter 6, Securing REST Endpoints Using Authorization and Authentication, explains how you can secure these REST endpoints using Spring Security. You’ll implement token-based authentication and authorization for REST endpoints. Successful authentication will provide two types of tokens – a JSON Web Token (JWT) as an access token, and a refresh token in response. The JWT-based access token then will be used to access secured URLs. A refresh token will be used to request a new JWT if the existing JWT has expired. A valid request token can provide a new JWT to use. You’ll associate users with roles such as Admin, User, and so on. These roles will be used as authorization to make sure that REST endpoints can only be accessed if the user holds a certain role. We’ll also briefly discuss cross-site request forgery (CSRF) and cross-origin resource sharing (CORS).

Chapter 7, Designing a User Interface, concludes the end-to-end development and communication between different layers of the online shopping app. This UI app will be a single-page application (SPA) that consists of interactive components such as Login, Product Listing, Product Detail, Cart, and Order Listing. By the end of the chapter, you will have learned about SPA and UI component development using React and consuming REST APIs using the browser’s in-built Fetch API.

Chapter 8, Testing APIs, introduces manual and automated testing of APIs. You will learn about unit and integration test automation. After learning about automation in this chapter, you will be able to make both types of testing an integral part of the build. You will also set up the Java code coverage tool to calculate the different code coverage metrics.

Chapter 9, Deployment of Web Services, explains the fundamentals of containerization, Docker, and Kubernetes. You will then use this concept to containerize the example e-commerce app using Docker. This container will then be deployed in a Kubernetes cluster. You are going to use minikube for Kubernetes, which makes learning and Kubernetes-based development easier.

Chapter 10, Getting Started with gRPC, introduces the gRPC fundamentals.

Chapter 11, gRPC API Development and Testing, implements gRPC-based APIs. You will learn how to write a gRPC server and client, along with writing APIs based on gRPC. In the latter part of the chapter, you will be introduced to microservices and how they will help you to design modern, scalable architecture. Here, you will go through the implementation of two services – a gRPC server and a gRPC client.

Chapter 12, Adding Logging and Tracing to Services, explores the logging and monitoring tool called the Elasticsearch, Logstash, Kibana (ELK) stack, and Zipkin. These tools will then be used to implement the distributed logging and tracing of the request/response of the API calls. You will learn how to publish and analyze the logging and tracing of different requests and logs related to responses. You will also use Zipkin to monitor the performance of API calls.

Chapter 13, Getting Started with GraphQL, talks about the fundamentals of GraphQL – the schema definition language (SDL), queries, mutations, and subscriptions. This knowledge will help you in the next chapter, where you will implement an API based on GraphQL. Over the course of this chapter, you will learn about the basics of the GraphQL schema and solving the N+1 problem.

Chapter 14, GraphQL API Development and Testing, explains GraphQL-based API development and its testing. You will implement GraphQL-based APIs for an example application in this chapter. A GraphQL server implementation will be developed based on the design-first approach.

To get the most out of this book

Ensure you have the following hardware and software:

Software/hardware covered in the book

Operating system requirements

Java 17

Windows, macOS, or Linux (any)

Any Java IDE such as Netbeans, IntelliJ, or Eclipse

An internet connection to clone the code from GitHub and download the dependencies and libraries

Docker

Kubernetes (minikube)

cURL or any API client such as Insomnia

Node 18.x

VS Code

The ELK stack and Zipkin

Each chapter will contain special instructions to install the required tools if applicable.

If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book’s GitHub repository (a link is available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

Download the example code files

You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Modern-API-Development-with-Spring-6-and-Spring-Boot-3. If there’s an update to the code, it will be updated in the GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Conventions used

There are a number of text conventions used throughout this book.

Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: “If we are using Link in any of the models, then the generated models would use the mapped org.springframework.hateoas.Link class instead of the model defined in the YAML file.”

A block of code is set as follows:

 const Footer = () => {   return (
     <div>
       <footer
         className="text-center p-2 border-t-2 bggray-
           200 border-gray-300 text-sm">
         No &copy; by Ecommerce App.{" "}
         <a href=https://github.com/PacktPublishing/Modern- 
           API-Development-with-Spring-and-Spring-Boot>
           Modern API development with Spring and Spring Boot
         </a>
       </footer>
     </div>
   );
 };
 export default Footer;

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

<Error>  <errorCode>PACKT-0001</errorCode>
  <message>The system is unable to complete the request. 
      Contact system support.</message>
  <status>500</status>
  <url>http://localhost:8080/api/v1/carts/1</url>
  <reqMethod>GET</reqMethod>
</Error>

Any command-line input or output is written as follows:

$ curl --request POST 'http://localhost:8080/api/v1/carts/1/items' \ --header 'Content-Type: application/json' \ 
 --header 'Accept: application/json' \ 
 --data-raw '{ 
 "id": "1", 
 "quantity": 1, 
 "unitPrice": 2.5 
 }'
[]

Bold: Indicates a new term, an important word, or words that you see onscreen. For instance, words in menus or dialog boxes appear in bold. Here is an example: “Select System info from the Administration panel.”

Tips or important notes

Appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, email us at customercare@packtpub.com and mention the book title in the subject of your message.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata and fill in the form.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packt.com with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Share Your Thoughts

Once you’ve read Modern API Development with Spring 6 and Spring Boot 3, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.

Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.

Download a free PDF copy of this book

Thanks for purchasing this book!

Do you like to read on the go but are unable to carry your print books everywhere?
Is your eBook purchase not compatible with the device of your choice?

Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.

Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application. 

The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily

Follow these simple steps to get the benefits:

  1. Scan the QR code or visit the link below

https://packt.link/free-ebook/9781804613276

  1. Submit your proof of purchase
  2. That’s it! We’ll send your free PDF and other benefits to your email directly
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Modern API Development with Spring 6 and Spring Boot 3 - Second Edition
Published in: Sep 2023Publisher: PacktISBN-13: 9781804613276
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Sourabh Sharma

Sourabh Sharma is a Senior Development Manager at Oracle with over 20 years of experience in the industry. He is a manager and architect who has been designing on-premise and cloud-based applications using Java, Javascript, and Oracle DB. Sourabh has worked with leading companies and delivered enterprise products and applications. His expertise lies in conceptualizing, modeling, designing, and developing N-tier and cloud-based web applications while leading teams. Sourabh's experience also includes developing microservice-based solutions and implementing various types of workflow and orchestration engines. He believes in continuous learning and sharing knowledge through his books and training.
Read more about Sourabh Sharma