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

Compartir libro
  1. 228 páginas
  2. English
  3. ePUB (apto para móviles)
  4. Disponible en iOS y Android
eBook - ePub

Building RESTful Web Services with Spring 5 - Second Edition

Raja CSP Raman, Ludovic Dewailly

Detalles del libro
Vista previa del libro
Índice
Citas

Información del 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.

Preguntas frecuentes

¿Cómo cancelo mi suscripción?
Simplemente, dirígete a la sección ajustes de la cuenta y haz clic en «Cancelar suscripción». Así de sencillo. Después de cancelar tu suscripción, esta permanecerá activa el tiempo restante que hayas pagado. Obtén más información aquí.
¿Cómo descargo los libros?
Por el momento, todos nuestros libros ePub adaptables a dispositivos móviles se pueden descargar a través de la aplicación. La mayor parte de nuestros PDF también se puede descargar y ya estamos trabajando para que el resto también sea descargable. Obtén más información aquí.
¿En qué se diferencian los planes de precios?
Ambos planes te permiten acceder por completo a la biblioteca y a todas las funciones de Perlego. Las únicas diferencias son el precio y el período de suscripción: con el plan anual ahorrarás en torno a un 30 % en comparación con 12 meses de un plan mensual.
¿Qué es Perlego?
Somos un servicio de suscripción de libros de texto en línea que te permite acceder a toda una biblioteca en línea por menos de lo que cuesta un libro al mes. Con más de un millón de libros sobre más de 1000 categorías, ¡tenemos todo lo que necesitas! Obtén más información aquí.
¿Perlego ofrece la función de texto a voz?
Busca el símbolo de lectura en voz alta en tu próximo libro para ver si puedes escucharlo. La herramienta de lectura en voz alta lee el texto en voz alta por ti, resaltando el texto a medida que se lee. Puedes pausarla, acelerarla y ralentizarla. Obtén más información aquí.
¿Es Building RESTful Web Services with Spring 5 - Second Edition un PDF/ePUB en línea?
Sí, puedes acceder a Building RESTful Web Services with Spring 5 - Second Edition de Raja CSP Raman, Ludovic Dewailly en formato PDF o ePUB, así como a otros libros populares de Informatica y Programmazione in Java. Tenemos más de un millón de libros disponibles en nuestro catálogo para que explores.

Información

Año
2018
ISBN
9781788471879
Edición
2
Categoría
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...

Índice