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

Condividi libro
  1. 228 pagine
  2. English
  3. ePUB (disponibile sull'app)
  4. Disponibile su iOS e Android
eBook - ePub

Building RESTful Web Services with Spring 5 - Second Edition

Raja CSP Raman, Ludovic Dewailly

Dettagli del libro
Anteprima del libro
Indice dei contenuti
Citazioni

Informazioni sul libro

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.

Domande frequenti

Come faccio ad annullare l'abbonamento?
È semplicissimo: basta accedere alla sezione Account nelle Impostazioni e cliccare su "Annulla abbonamento". Dopo la cancellazione, l'abbonamento rimarrà attivo per il periodo rimanente già pagato. Per maggiori informazioni, clicca qui
È possibile scaricare libri? Se sì, come?
Al momento è possibile scaricare tramite l'app tutti i nostri libri ePub mobile-friendly. Anche la maggior parte dei nostri PDF è scaricabile e stiamo lavorando per rendere disponibile quanto prima il download di tutti gli altri file. Per maggiori informazioni, clicca qui
Che differenza c'è tra i piani?
Entrambi i piani ti danno accesso illimitato alla libreria e a tutte le funzionalità di Perlego. Le uniche differenze sono il prezzo e il periodo di abbonamento: con il piano annuale risparmierai circa il 30% rispetto a 12 rate con quello mensile.
Cos'è Perlego?
Perlego è un servizio di abbonamento a testi accademici, che ti permette di accedere a un'intera libreria online a un prezzo inferiore rispetto a quello che pagheresti per acquistare un singolo libro al mese. Con oltre 1 milione di testi suddivisi in più di 1.000 categorie, troverai sicuramente ciò che fa per te! Per maggiori informazioni, clicca qui.
Perlego supporta la sintesi vocale?
Cerca l'icona Sintesi vocale nel prossimo libro che leggerai per verificare se è possibile riprodurre l'audio. Questo strumento permette di leggere il testo a voce alta, evidenziandolo man mano che la lettura procede. Puoi aumentare o diminuire la velocità della sintesi vocale, oppure sospendere la riproduzione. Per maggiori informazioni, clicca qui.
Building RESTful Web Services with Spring 5 - Second Edition è disponibile online in formato PDF/ePub?
Sì, puoi accedere a Building RESTful Web Services with Spring 5 - Second Edition di Raja CSP Raman, Ludovic Dewailly in formato PDF e/o ePub, così come ad altri libri molto apprezzati nelle sezioni relative a Informatica e Programmazione in Java. Scopri oltre 1 milione di libri disponibili nel nostro catalogo.

Informazioni

Anno
2018
ISBN
9781788471879
Edizione
2
Argomento
Informatica

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

Indice dei contenuti