Building RESTful Web Services with Spring 5 - Second Edition
eBook - ePub

Building RESTful Web Services with Spring 5 - Second Edition

Raja CSP Raman, Ludovic Dewailly

Partager le livre
  1. 228 pages
  2. English
  3. ePUB (adapté aux mobiles)
  4. Disponible sur iOS et Android
eBook - ePub

Building RESTful Web Services with Spring 5 - Second Edition

Raja CSP Raman, Ludovic Dewailly

DĂ©tails du livre
Aperçu du livre
Table des matiĂšres
Citations

À propos de ce livre

Find out how to implement the REST architecture to build resilient software in Java with the help of the Spring 5.0 framework. About This Book‱ Follow best practices and explore techniques such as clustering and caching to achieve a reactive, scalable web service, ‱ Leverage the Spring Framework to quickly implement RESTful endpoints, ‱ Learn to implement a client library for a RESTful web service using the Spring Framework along with the new front end framework.Who This Book Is ForThis book is intended for those who want to learn to build RESTful web services with the latest Spring 5.0 Framework. 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 would also help you get up and running quickly.What You Will Learn‱ Deep dive into the principles behind REST‱ Expose CRUD operations through RESTful endpoints with the Spring Framework‱ Devise response formats and error handling strategies, offering a consistent and flexible structure to simplify integration for service consumers‱ Follow the best approaches for dealing with a service's evolution while maintaining backward compatibility‱ Understand techniques to secure web services‱ Comply with the best ways to test RESTful web services, including tips for load testing‱ Optimise and scale web services using techniques such as caching and clusteringIn DetailREST 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.Style and approachReaders will be taken through a set of specific patterns and tested practices to build effective RESTful systems in Java using the Spring framework.

Foire aux questions

Comment puis-je résilier mon abonnement ?
Il vous suffit de vous rendre dans la section compte dans paramĂštres et de cliquer sur « RĂ©silier l’abonnement ». C’est aussi simple que cela ! Une fois que vous aurez rĂ©siliĂ© votre abonnement, il restera actif pour le reste de la pĂ©riode pour laquelle vous avez payĂ©. DĂ©couvrez-en plus ici.
Puis-je / comment puis-je télécharger des livres ?
Pour le moment, tous nos livres en format ePub adaptĂ©s aux mobiles peuvent ĂȘtre tĂ©lĂ©chargĂ©s via l’application. La plupart de nos PDF sont Ă©galement disponibles en tĂ©lĂ©chargement et les autres seront tĂ©lĂ©chargeables trĂšs prochainement. DĂ©couvrez-en plus ici.
Quelle est la différence entre les formules tarifaires ?
Les deux abonnements vous donnent un accĂšs complet Ă  la bibliothĂšque et Ă  toutes les fonctionnalitĂ©s de Perlego. Les seules diffĂ©rences sont les tarifs ainsi que la pĂ©riode d’abonnement : avec l’abonnement annuel, vous Ă©conomiserez environ 30 % par rapport Ă  12 mois d’abonnement mensuel.
Qu’est-ce que Perlego ?
Nous sommes un service d’abonnement Ă  des ouvrages universitaires en ligne, oĂč vous pouvez accĂ©der Ă  toute une bibliothĂšque pour un prix infĂ©rieur Ă  celui d’un seul livre par mois. Avec plus d’un million de livres sur plus de 1 000 sujets, nous avons ce qu’il vous faut ! DĂ©couvrez-en plus ici.
Prenez-vous en charge la synthÚse vocale ?
Recherchez le symbole Écouter sur votre prochain livre pour voir si vous pouvez l’écouter. L’outil Écouter lit le texte Ă  haute voix pour vous, en surlignant le passage qui est en cours de lecture. Vous pouvez le mettre sur pause, l’accĂ©lĂ©rer ou le ralentir. DĂ©couvrez-en plus ici.
Est-ce que Building RESTful Web Services with Spring 5 - Second Edition est un PDF/ePUB en ligne ?
Oui, vous pouvez accĂ©der Ă  Building RESTful Web Services with Spring 5 - Second Edition par Raja CSP Raman, Ludovic Dewailly en format PDF et/ou ePUB ainsi qu’à d’autres livres populaires dans Informatica et Programmazione in Java. Nous disposons de plus d’un million d’ouvrages Ă  dĂ©couvrir dans notre catalogue.

Informations

Année
2018
ISBN
9781788471879
Édition
2

Ticket Management – Advanced CRUD

Our application has to meet real-time business cases, such as Ticket management. This chapter will review most of the topics covered in the book's previous chapters.
In this chapter, we will create a real-time scenario and implement the business requirements for our scenario—Ticket management by the user, customer service representative (CSR), and admin.
Our final chapter includes the following topics:
  • Creating a ticket by customer
  • Updating the ticket by customer, CSR, and admin
  • Deleting the ticket by customer
  • CSR/admin deletes multiple tickets

Ticket management using CRUD operations

Before moving on to the Ticket Management System, we will cover business requirements.
Let's say we have a banking web application that can be used by our customers, Peter and Kevin, and we have Sammy, our admin, and Chloe, the CSR, to help in case of any application issues.
Peter and Kevin are facing some problems in the payment process. When they try to click on the payment transaction submit button, it's not working. Also, the transaction view is in a web page. So our users (Peter and Kevin) will create a ticket to share their problem.
Once the ticket is created, it can be updated by customer/CSR/admin. Also, a customer can delete their own ticket. While updating, anyone can change the severity; however, only CSR and admin can change the status, as the ticket's status is related to official activities.
Customers can view their tickets in total or as a single ticket, but they can delete only one ticket at a time. The Multi-delete option is available for both CSR and admin. However, CSR can only delete three tickets at once. Admin will have full control in the Ticket management application and can delete any number of tickets at any time.

Registration

Let's start our coding to fulfill the preceding requirements. At first, we need to start with customer, CSR, and admin registration. As these users have different roles, we will give different user types for each user.

User types

To differentiate users, we came up with three different user types so their authorization will be varied when they access our REST APIs. Here are the three different user types:
Name
User type
General user/customer
1
CSR
2
Admin
3

User POJO

In our previous User class, we only had the userid and username. We may need two more variables to fulfill the business requirements we mentioned earlier. We will add password and usertype to our existing User class:
private String password; 
/*
* usertype:
* 1 - general user
* 2 - CSR (Customer Service Representative)
* 3 - admin
*/
private Integer usertype;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsertype(Integer usertype){
this.usertype = usertype;
}
public Integer getUsertype(){
return this.usertype;
}
In the preceding code, we have just added password and usertype. Also, we have added getter and setter methods for our variables.
You can view the full User class on our GitHub repository (https://github.com/PacktPublishing/Building-RESTful-Web-Services-with-Spring-5-Second-Edition).

You may be tired of adding getter and setter methods, so we will replace them with Lombok library, which we will discuss later in this chapter. However, Lombok library has some conflict issues with Eclipse or STS IDE, which you might need to be aware of. In certain versions of these IDEs, you won't get expected behavior on class creation because of Lombok library issues. Also, some developers mentioned that they have deployment issues with Lombok.
In order to automatically generate user ID from our User class, we will use a separate counter. We will keep a static variable to do that; it's not recommended in real application to keep a static counter. To simplify our implementation logic, we have used the static counter.
The following code will be added to our User class:
private static Integer userCounter = 100;
We have started with 100 users. Whenever a new user is added, it will automatically increment the userid and assign it to the new user.
There is no restriction on the userCounter starting point. By keeping user series in 2 (2XX) and ticket in series 3 (3XX), it's easier for the reader to differentiate user and ticket.
Now we will create a new constructor to add the user to our application. Also, we shall increment the usercounter parameter and assign it as userid for each new user:
public User(String username, String password, Integer usertype) {
userCounter++;
this.userid = userCounter;
this.username = username;
this.password = password;
this.usertype = usertype;
}
The preceding constructor will fill all user details, including the userid (from usercounter).
Here, we will add a new user with username, password, and usertype in the UserServiceImpl class; usertype will vary for each user (for example, usertype for admin is 3):
 @Override
public void createUser(String username, String password, Integer usertype){
User user = new User(username, password, usertype);
this.users.add(user);
}
In the preceding code, we have created a new user and added it to the existing user list.
In the preceding code, we didn't mention the abstract method in UserService. It is assumed that every concrete method will have an abstract method in the interface. Hereafter, consider adding all abstract methods in appropriate interfaces.

Customer registration

Now it is time to add a customer. A new customer will have to create an account by adding a username and password details.
We will talk about the customer registration API. This API will help any new customer to register their account with us:
 @ResponseBody
@RequestMapping(value = "/register/customer", method = RequestMethod.POST)
public Map<String, Object> registerCustomer(
@RequestParam(value = "username") String username,
@RequestParam(value = "password") String password
) {
userSevi...

Table des matiĂšres