Book Image

Building RESTful Web Services with Spring 5 - Second Edition

By : Raja CSP Raman, Ludovic Dewailly
Book Image

Building RESTful Web Services with Spring 5 - Second Edition

By: Raja CSP Raman, Ludovic Dewailly

Overview of this book

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.
Table of Contents (21 chapters)
Title Page
Copyright and Credits
Dedication
Packt Upsell
Contributors
Preface
6
Spring Security and JWT (JSON Web Token)
Index

Preface

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. Starting from the basics of the philosophy behind REST, this book goes through the necessary steps to design and implement 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 built-in MVC, as well as the frontend framework. 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.

Who this book is for

This book is intended for those who want to learn to build RESTful web services with the latest Spring Framework 5.0. 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 will also help you get up and running quickly.

What this book covers

Chapter 1A Few Basics, covers a basic understanding of REST, Reactive programming, and their basics, including the benefits of Reactive programming. It also covers Spring 5 basics with Reactive programming and an example RESTful web service as a base for other chapters.

Chapter 2Building RESTful Web Services in Spring 5 with Maven, covers how to build a RESTful web service with Apache Maven by using either the Eclipse IDE or STS (Spring Tool Suite). The second section of the chapter covers creating a new project in Eclipse/STS and running our basic REST API.

Chapter 3Flux and Mono (Reactor Support) in Spring, discusses Reactive programming and its benefits. This chapter also covers a little bit about Reactive Core and Reactive Streams. The second section of this chapter covers Flux and Mono in Spring REST, including a basic implementation of the GET and POST methods in Reactive.

Chapter 4CRUD Operations in Spring REST, covers mapping CRUD operations to HTTP methods and implementation of CRUD operations on User with Reactor support.

Chapter 5CRUD Operations in Plain REST (Without Reactive) and File Upload, covers mapping CRUD operations to HTTP methods and implementation of CRUD operations on User (Create, Read, Update, and Delete) without Reactor support. Also, this chapter covers file uploading in Spring.

Chapter 6Spring Security and JWT (JSON Web Token), covers Spring Security, JWT (JSON Web Token), and JWT generation. The second section of this chapter covers getting details from the generated token and also restricting service calls by JWT security.

Chapter 7Testing RESTful Web Services, talks about various testing strategies to test our existing RESTful web services, including JUnit and MockMvc-like unit test cases, and clients such as Postman, SoapUI, and jsoup web reader.

Chapter 8Performance, discusses different performance-related topics, including HTTP compression, HTTP caching, and HTTP control. The second section of the chapter covers cache implementation and HTTP headers such as If-Modified-Since and ETag.

Chapter 9AOP and Logger Controls, covers Spring AOP, including its theory, implementation, and logging controls.

Chapter 10, Building a REST Client and Error Handling, covers RestTemplate in Spring, the basic setup for building a RESTful service client with Spring, and calling the RESTful service from the client side. The second section of the chapter covers error handling, including defining an error handler and using it.

Chapter 11Scaling, covers the techniques, libraries, and tools used for application scaling purposes. It includes clustering and the benefits of clustering. This chapter also covers load balancing, scaling a database, and distributed caching.

Chapter 12Microservice Basics, talks about microservices, the benefits of microservices, and the basic characteristics of microservices. It also covers various microservice components.

Chapter 13Ticket Management - Advanced CRUD, covers advanced CRUD operations on tickets, including creating and updating a ticket through a customer, updating a ticket by CSR, and updating a ticket by admin. This chapter also talks about deleting multiple tickets by CSR and admin.

To get the most out of this book

The following is a descriptive list of the requirements to test all the code in this book:

  • Hardware: 64-bit machine with minimum 2 GB RAM and min 5 GB of free hard disk space
  • Software: Java 9, Maven 3.3.9, STS (Spring Tool Suite) 3.9.2 
  • Java 9: All code is tested on Java 9
  • SoapUI: SoapUI 5.2.1 (free version) is used for REST API calls
  • Postman: For REST client testing, Postman 5.0.4 is used

 

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packtpub.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Building-RESTful-Web-Services-with-Spring-5-Second-Edition. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/BuildingRESTfulWebServiceswithSpring5_ColorImages.pdf.

Conventions used

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

CodeInText: 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: "Let's add a Logger to the class; in our case, we can use UserController."

A block of code is set as follows:

@ResponseBody
  @RequestMapping("/test/aop/with/annotation")
  @TokenRequired
  public Map<String, Object> testAOPAnnotation(){
    Map<String, Object> map = new LinkedHashMap<>();
    map.put("result", "Aloha");   
    return map;
  }

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

2018-01-15 16:29:55.951 INFO 17812 --- [nio-8080-exec-1] com.packtpub.restapp.HomeController : {test} info
2018-01-15 16:29:55.951 WARN 17812 --- [nio-8080-exec-1] com.packtpub.restapp.HomeController : {test} warn
2018-01-15 16:29:55.951 ERROR 17812 --- [nio-8080-exec-1] com.packtpub.restapp.HomeController : {test} error

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

mvn dependency:tree

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Now you can generate the project by clicking Generate Project."

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

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/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

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 [email protected] 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.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.