Spring Boot 3 | Curso Completo 2023

152.07k views14578 WordsCopy TextShare
Michelli Brito
Semana Decoder - Evento GRATUITO de 23 a 26 de Outubro para você aprender Arquitetura de Microservic...
Video Transcript:
Hello, welcome, very welcome here on the channel. I'm Michelli Brito and in this video today we're going to build together. Step by step a RESTful API using the new versions of Spring Boot. Three Spring Framework six in Java 17. But first of all, I want to break the news to you. I just posted the fourth edition of the free ebook Extreme Boot Data and Web to Microservices. To download it is just you then access the link that is here in the description. This ebook has much of the content that we are going to see now
during that time and much more, involving concepts, definitions from Spring Bean as well as Spring Framework container. Also about API, maturity models and also Rest architecture to Microservices with Spring Boot. So for you to check out all this content, just download this ebook through the link here in the description. And now let's start building, getting our hands dirty too. In a little while on this complete Rest API involving all the methods of a CRUD and of course, following Richard's maturity model so that we can finally reach a REST API. But first of all, let's take
a look, a quick overview of the Spring ecosystem and its various projects that we can use on a daily basis in our Java application development. So, first of all I want to introduce you here quickly and talk a little bit about the Spanish ecosystem, where Spring Boot is one of the projects that are part of this ecosystem as a whole. So here I put some of the main projects that make up this Spring ecosystem, such as Spring Boot that we are going to use now to start starting our API and create this entire implementation throughout
this video. Also Spring Security that we can use, for example, to implement authentication and authorization in our Spring cloud applications with several subprojects, including, that allows us to build complete Microservices architectures and spring data to work with databases from different models of Spring Web databases, which is what we're going to use to build our API with the Spring ecosystem. And we can also work with messaging and loads. And of course, down here is the foundation of Spring, which is the Spring Framework that makes all of this possible. So here I just wanted to show you
that the ecosystem involves several different projects that allow us to explore this ecosystem a lot in our ABA applications, from a web application to the creation of an API, as we are going to do now, to the creation of a Microservices architecture, for example. Working with events, messaging, loads, reactive programming, among many other things. Then also, if you're curious, I'll leave the link in the link here in the description and the Spring documentation so you can navigate through these different projects. And here in the sequence I mentioned some of the projects, but the Spring ecosystem
involves many others, right And as I said earlier, Spring, the Spring ecosystem, it has a project that is actually the basis of the ecosystem as a whole, which is the Spring Framework. It is composed of modules such as Data Access Web, programming oriented to instrumental aspects of message. And here below, the Core Container stands out, which has its subprojects such as Bean Core, Context, among others, which is really where the classes are, the interfaces that are the basis of this entire ecosystem and where we also have the implementation of the inversion of control, using, in
the case of Spring, dependency injection. Then also if you want to know more in detail how the whole Spring Framework works, how the entire life cycle of this core container works How it deals with each of your assets You just have to access the ebook that is the link here in the description, because there you will find the material a detailed Bean content about all these concepts and definitions. And following here, let's now quickly talk about this in About, which is one of the projects that make up the Spring ecosystem that we can use at
the start of an application where we can create everything from a web application to each of the Microservices, for example, that make up an architecture of a much faster way. In a simplified way. Why Because with Spring Boot we are able to start this process faster, since we don't need to make many of the initial configurations of an application, such as configuring a dispatch Servlet. Also do all the configuration of a servlet container like Tomcat or net, do all the XML configurations or configuration classes so that later we can really start implementing the business rules.
So with Spring Boot we already have this facility, because many of these configurations are already ready by default. Of course, we can customize when necessary, but these settings come ready-made and so we can quickly start the application and really focus on our business rules and each of these applications. So, in a nutshell Bean, the Spring Boot, it is the sum of the Spring Framework, since it uses the entire base of the Spring ecosystem which is the Spring Framework behind it, so it is the sum of Spring, plus an embedded server, since it relies on a
servlet. Contains embedded. Depending on what we are going to use, it can be a Tomcat, it can be a net for example, minus the configurations via XML files or also via configuration classes. So in short, we have a start and ease in creating Java applications with the Spring ecosystem and Spring Boot. Over the years it has undergone several improvements, updates and recently we had a major update that went from version two point X to version 3.5, which we are going to use here. During this video here I put it for you. This is an image
that I also took from the official documentation of Spain Build that we can see as Spring Boot has evolved over the last few years. Regarding your versions. And all these flags here in green and yellow also show in terms of time, the period of support, both community support and also commercial support for Spanish from here to find out at the same level of curiosity. And now So let's talk a little bit about this version of Spring Boot three, this new version that came out recently. One of the main changes that came in this new version
is in relation to the Java baseline, where for you to use Spring Boot 3 you need to be using at least Java 17, which is the latest LTS version of the JDK. So if you are using an earlier version, for example 11 14, 16, you will not be able to use Spring Boot three. So, to make this use, it requires as a minimum requirement the LTS version, JDK 17 or higher. It's the same as for compatibility issues as well, it requires Jakarta and version nine or higher, both Spring Boot three and Spring Framework six. The
foundation of the entire Spring ecosystem, which has also been updated from version 5.6 to version 6.5. And just like the Spring Framework it also requires Java 17. And we also had naming changes of all the Java packages and the JavaFX packages which were renamed to Jakarta. So, for example, when we used JavaFX Validation, VAX, JavaFX Persistence, now you'll see during our implementation that we're going to continue using the same property dependencies. But now our imports will be made using the nomenclature Jakarta, Validation, Jakarta, Persistence, Jakarta Servlet, among others. We also had official support for native
image with grade of M, which is the way we can start an application faster, also using less memory usage, also achieving greater performance. So now we have this official support and also the new project. The new observe skill that has been added to the Spring ecosystem. With this new change, in this case, the Micro Mira project brings. In this case, we previously had a project called Spring Cloud Island, where we could, in a quick and simple Bean way, also implement distributed tracking in our applications. And now this project exposes Cloud Island. It stopped being updated,
it will be discontinued and the project that will now remain official for Observable, that is, for us to also continue inserting Observable for distributed tracking and even using the ZIP tool, which, for example, is the most promising, brings right, among many other improvements and updates that have in this new version of Spring Boot. Three, but we're going to see many of these now in practice during the implementation of our RESTful API. Ok So now let's start implementing this REST API. Let's follow Richard's maturity model, its four levels, using this new version and Spring Boot three.
But to make it a little easier to visualize what we are going to implement from now on, I will present here the API that we are going to build. In this case it will be a product API Product API. The rest we will use here. All these Spring projects that are listed here on the left, such as Spring Boot. Three. To start and create this entire application, we will also use Java A17 to return this web application, then we will use Spring Web MVC and also two candidates to adapt to work. So with the database,
saving our resources and among many other operations, Spring Validation to do some initial validations of our API and also with Spain, which is also one of the Spring Framework projects of the Spring ecosystem as a whole, which it allows us to implement hypermedia and thus satisfy another level of Wish maturity. There are many cases and many APIs do not have this implementation at all. And often what really matters is good API documentation so that all your customers can easily connect there and consume all the endpoints. But taking into account Richardson's maturity levels, which makes a
web API. I will also contemplate here the implementation of watchOS to stay there as an example and also as a learning experience. When there is a need for implementation. So here next to this right side, I've listed all the endpoints that we're going to implement that and nothing more than our complete CRUD involving methods, bridge to client creation also reading methods involving the http get methods, also methods for update, updates and also deletion. Alright So from now on, let's start getting our hands dirty. Call it Cloud and that's how we're going to build this entire
REST API. Remembering that for those who are curious and also want to get more involved on this subject of West APIs, this whole concept, this architectural model, this REST architecture, mainly also involving the constraints proposed and defined by Ray Fielding and also understanding a little more about the model of maturity proposed by Leonard Richardson. You will find all this content in detailed Bean form in the ebook in the fourth edition and this updated version 2023 that the link is here in the description. So now let's get down to business. So, first of all, we need
to have our environment prepared. As I mentioned earlier, we are going to use Java 17. So I already came here on the Oracle website, I already downloaded it, I installed Java A17 on my machine, so you need to follow all this same step by step to have this environment prepared for the development. And if you later come here on the Oracle website, you will be able to visualize JDK seven, which is this last version LTS that is available, i.e. the long term version. Mainly, we must be very careful and cautious in choosing the version that
we are going to choose for a certain software for a certain application, because depending on the versions, we have short-term versions that have been updated every six months for the last few years. And we also have the LTS versions, which are these Java versions, which have long-term support and are the most appropriate, especially when we are going to put an application, a system in production. So in this case, I'm going to use the JDK 17 LTS version here, right. And to start this Spring Boot application, we're going to use the platform's website. It's Spring Initialize
So there are other ways to start a Spring Boot application for those who are using the idea , for example, we can do it directly inside the idea. But now we're going to do it here in a more generic way, but I'll also leave it here because regardless of which idea you're going to be using, you can start here by initialize and then import it to the idea you want. So, Spring initialize with just a few definitions and a few choices here, we'll quickly be able to start a project here, this ping boot three. So
here inside the first initialize, the first option we need to define is which will be the dependency manager that we are going to use here. In this case, I selected the month in which we are going, then from the XML file and then we are going to add all the dependencies, both initial and the dependencies that we will need in the course of this implementation. Then here we need to define what the language will be, in this case Java and here below the Spring Boot version. Always here you will see that it already has the
current version selected, the current version and you can see that we are already in version 3.11 point zero two Spring Boot. So, as it was already selected by default, we are going to use version 3.1 point zero of Spring Boot and here below we are going to fill in some metadata, such as Help, etc. In m description and among other things. In case help. I'll leave it the way it's good with the point. Example and down here in the Architect, instead of the demo I'm going to put Spring Boot, so it already shows what the
complete PAC of our application with a period will be. Example point is Spring Boot. Here what will be the type of packaging, in this case the jar And down here what is the version of Java we are using, in this case the Java 17 check. That done, now we need to select some initial dependencies and here in a simple Bean way, we are already able to insert in this project some dependencies that we will start to need from the beginning of the implementation. I'm going to click here on Add Dependencies. The first one will be
deleting the dependency, since we are going to build our RESTful API here in the description of this project it already shows the construction of web applications, including RESTful, applications using Spring MVC. When I select it, it already shows me this dependency here in the list of selected dependencies. After that I will also select the Spring Data J Pear dependency, since we are going to use a SQL database and I will also add the post dependency with this, right Driver post SQL for those who do not want to download and install the entire infrastructure of Postgres
on your machine so that you can then work with this database, you can, for example, as an option, use H2, which will also have the same result and is a little simpler, but to take advantage of this whole video we'll go also work with the post. Then I leave the link there for you. From everything you need to have installed to prepare the environment. Well, it's one more dependency that we're going to insert in relation to Validation, so we're going to use it here too this dependency to include some initial validations in our API before
we save a given resource. So these are the initial dependencies that we are going to use. And now within the super initialize we can already see a preview of this project, taking into account all the selections and options that we previously defined here below in explore. If we click, it will show me here, for example, our file by an XML point. So the preview of this file will look like this. Here you can see that the version is expanded, BUT three dots to 1.0. Here we already have the Java 17 version and here below all
the initial dependencies we just selected Spring Data JPEG Validation Web and also the Postgres dependency. So you can see that, for example, we're using the Spring Data project and how we're making use of the space. But. We just need to use the Spring Boot Starter dependency, footer date and then we will be able to use all the dependencies of this project to carry out our transactions. Modeling our data to our database, right Same thing Spring Boot Starter Validation. We will be able to use many of the annotations to validate our classes and, of course, the
Spring Boot Starter Web that makes all the construction of our web application possible, that is, our application and our web API, right . uses this simplifier, let's say Spring Boot is starter and the dependency is in relation to a certain project that we are going to insert in our application. Here too it shows files, guides and documentation references regarding dependency aliases that we have already selected earlier. So it's at the level of curiosity for anyone who wants to go deeper or see tutorials on each of these topics. And now I'm going to come back here,
I'm going to close and I'm going to click on the button to generate this project. And what's going to happen ? In my case I'm going to use the smart one, right So now I'm going to generate this project here. You can see that a download has already started and the next steps will unzip this file and also import it into the smart idea. So here I am in a little folder where I'm putting all the Spring projects. I've already unzipped it, it's already here Spring Boot and now let's import this project into the idea.
I'm using Intelligent File Open and now here I'm going to open this project, in this case Spring Boot, which is the name I selected earlier. I'm going to select the XML file here to open it and then the month will already download all the necessary dependencies. Open as a project for this window. And now let's wait here for a while so that it can then download and download all the initial dependencies that we selected. But later you and we will be able to see that in addition to those dependencies, if I'm not mistaken, there are
four or five dependencies that we selected. Many more dependencies will be downloaded. Why Because a dependency it often requires sub dependencies of other projects. So, when downloading Spring Boot from behind, it will also download all the necessary Spring Framework dependencies, such as Binds Context, among others. So I think the month is already over. First of all, I'm going to open the libraries here and here we can see all these dependencies that it just downloaded and made available here in our project. First of all, it is using Java 17, as we have already selected, and here
you can see that it has many dependencies that we did not select, but which are available here. In our application, for example, dependencies related to logs, dependencies down here. All those from Spring Boot and starter, so you can see that we are using version 3.1 point zero just right. Down here also the dependencies that I mentioned in relation to the Spring Framework, which is the basis of this entire ecosystem and Spring as a context core test JDBC, Bean aspects, among others all in versions look at 6.0 point nine, which is the current version from Spring
Frame, right So now it's just curiosity level. Afterwards, you can look at everything that was done in your project. Download. Let's take a look here at our XML file. Previously, we had already seen the preview inside initialize and now it is the official file inside our idea of ​​our application, with all the same properties that we had previously defined. Also the initial dependencies and here on the side we are going to visualize how the initial structure of this project of a project in Spring Boot turned out. So you can see that at the root we
have the file for an XML point where all these dependencies and other configurations of versions of this project are declared . And here, inside source Bean Java, we can see the complete patch that we also defined there in the initialize here and with the dot example dot is Spring Boot and at the root of this package we have this class ready, which is a class that is generated with the Spring Boot project, which is the class, Bean, let's say, but here in this case it takes the name of my project, of my application. In the
case of the boot Application that comes with the @ Spring Boot application annotation to show that this is the main class of the application, where we can start this application using, in the case of Intelligent, these shortcut buttons, these players in green. So we can use each of these shortcuts to start quickly and start this application. Another thing that it already has is this sources package with the Application Properties file that we are going to use to define database configurations. For example, if you are using queues, among many other things we can use this file
to create these definitions and configurations. And he also already brings here some packages with aesthetics and template. In this case, if we were building an MVC application with the Bio layer, we could also use these packages to insert our views here, our user interfaces. But since we are not going to make any views, we are not going to implement any views. We are implementing an API where we are going to make all calls via post Bean or via the same browser. So let's initially delete these folders here and this one is the initial structure of
our Spring Boot three set project. And now that we already have this initial structure, let's start the configurations, such as configuring the database, since as we have an XML point in the file, the dependency is Spring boot Starter data JPEG, it when I start this application it will already be waiting for a connection to a database, so that's why this is the first thing we're going to do here, let's make this connection right away so that everything is right and then we can continue with our implementation of our RESTful API. So here. First of all,
I'm going to show you here to open the pg admin which is the database that I'm going to use and it uses this interface and me so we can have access to all the databases that are created, the tables , Among other things. And here you can see that I've already started the Products API base, so this one here is a base that is already available and then we'll just come here and visualize it. All the mapping that we are going to do is via code using the JPG that will be created here as tables,
right For now, the first step is for you to enter the interface and start a new database. In the case here you see Database Create database and here you put the name you prefer there to create your database and make it available on your machine so that now in the next step we can make the connection with this database, right So , here within the application a Properties point, let's start some definitions for this political database connection. First, using the DataSource here, where it already brings here some attributes that we have to fill in for
this connection to be carried out. In the first line here we have Spring from the Source era, the RL, that is, we need to pass this connection RL with our database on our local machine, for example, as in my case I am using it or in the cloud or wherever you you are using your database, in this case we have to pass the URL of full jdbc, postgres, sql colon slash local slash, route. The fire port here from my machine to the database and A5432 slash is the name of the slash is the name of
this base that I just created that I showed you earlier in the pg admin, in this case Products API. Let's just check here if the name is correct. Products API dash. So far ok The next step for line two we now need to define the credentials so that our application can really connect with our political database and for that it will need to authenticate with username and password, as these credentials are defined during installing post on your machine. So here I defined that mine is username and lastly my password and my password is bank one,
two, three and we also use the same Spring Spring structure from the source era and username for created username and also password for our password created so that he can then authenticate himself and have this connection successfully made with our database. Down here we are going to use in line four of ping J, pear, Hibernate DDL auto update. What does this mean In this case, we can use the update client so much that I am configuring this auto DDL, that is, all the mapping that we are going to do via codes when we are going
to create our model, for example, I am enabling all this mapping made in Java, it is created through tables in the database, so later on we will create, for example, the entity, the mode and the product. Let's then map its attributes to each of these attributes. For example, let's take the database columns from the products table. So, in this way, whenever we start an application that we find the application, it, if this table does not exist in the database, it will create it automatically, as well as all the necessary columns in relation to each of
these attributes. Rules that we put, keys, among other things, and not just create too. If there is any update to this modeling, it will also include this in our possible database. But of course, for those who want to use tools to generate tables, columns and even generate these tables by hand, it's up to you to choose. And now here on the next line, line six to leave a little space to make it easier to visualize. I put here a property Spring JPEG, Properties, Hibernate, JDBC, lob. I don't know contextual like I think. So here I
am enabling it so that Hibernate logs are not created. Why Because only a few logs during application initialization and execution, Hibernate looks for Postgres metadata and in cases it does not find, for example, it will look like so many error logs in our console. So, to avoid these conflicts, I'm going to disable this creation of these Hibernate logs, using this definition here on line six. That done, we already have our connection to the database and now the next step is to start implementing our entity, our model, so that all tables, columns are created and we
already have our first entity mapped, using, of course, J pear with Java. And then this will reflect on our database and we will check to see if everything is correct and if it is being generated as expected. So here in our structure we have our root package here and inside this class it has to be at the root of the project, but to make it more suggestive and organized we are going to create some small ones here to divide the modules, details, controllers, repository , Among other things. So here at 1500 , I'm going to
create the first one here, which will be Models, where we're going to create our first model, which will be the module. So here now in this new package I'm going to create and click on New Java Test. Let's create our first class here and this class will be called Product Model. So let's build a product API as I showed on the previous slide, and remembering that we are going to create this API so that it is REST, following the maturity model defined by Leonard Richardson, which he then considers four levels for one to reach and
be considered the. But as we implement it, I'll show and quote each of these levels. Remembering that all the details of both this maturity model and the REST architecture, as well as the constraints defined by Field, you can find in the details in the ebook that is the description with the link here in the description is here Product Model. Let's start this new Java class and for it to be an entity in the database so that we can do this mapping from a Java class to an entity in the database, we need to use some
notes here for this that already facilitates this whole process, such as for example @, entity and also @table. Here in this case we need to pass the name and what will be the name of our table will be tb products tb. Anyway, products ok And that's done, we're also going to implement surrealism here. It's this interface where we show the JVM that this is a class that is able to undergo serialization, right ? the JVM. Here we are going to do the import and I am also going to define the serial version, the IDE here
at the beginning of this class, which is a version control number for each of these classes that can be carried out when necessary. Another thing that you can already observe is that these annotations that we used here before both @ before @ type for us to create this mapping of our Java class so that it is a table in the database, we use these two annotations here that belong to the Jakarta Persistence package. So here's one of the change points with this new release and Spring Boot three. Before, all these persistence annotations, for example, were
part of Java Persistence before Table, and now you can see that there was this naming change. So now they are part of the Jakarta Persistence package. Before and so on. Ok, now down here we are going to start the attributes of our model, which will be each of the columns of the table. So here our product module will have three attributes at first an int, the product ID, a name and also a value. So this product will have a name, a value and a unique identifier. So first of all, I'm going to do all the
necessary imports here. You can also see that the @id it belongs to the Jakarta Persistence package as well. Here we are going to import Generated Value, which is also part of Jakarta Persistence and here I am going to import Haiti and Big Destino. Well, our identifier which is the IDI of this class that will be the ID of our table. How do we demonstrate this In our mapping using the @ ID annotation and also using the @ generated value. This annotation that allows us to demonstrate and define how these identifiers will be generated. In this
case, I set Generic Generation Type to auto, that is, I won't need to start with the value for this ID every time we save each of the resources. In the case of products in the database, these sites are being generated and saved. Then each of these resources then we put and this auto generation of identifiers. Another very important point is that you can see that I am using an identifier like the ID of. This is a widely used identifier, especially when we have distributed architectures such as Microservices architecture, for example, which are used to identify
distributed pains and are highly recommended, especially for this type of architecture. When we have the database by Microservices, for example, because that way we avoid many conflicts, since the ID says they are universal. They can be generated anywhere and we won't run the risk of having a similar index, as we would have if we were using sequential indexes. So it is a very important point of attention, especially if we are also using distributed architectures. Name a private string name attribute of type string and a value for this product, in this case, a value of type
Big destination. And now that's done, having these initial attributes, I'm going to generate the getters and setters methods for each one of them. And since the idea already helps me here, I'll just select the one I want to generate. The getters and setters of all these attributes and quickly it will already generate these methods here for me. And now So we already have the model class with all the mapping is already perfect for our table to be generated in our database. Once that's done, now I'm going to upload the application . find this base for
it to connect with all the correct credentials , right ? So remembering to start this application we can use these shortcuts here too. Right-click Run Spring Boot Application. I'm going to start this way now, but there are several ways to start this application here, within the smart idea, the application quickly went up. I'm going to come back here just so we can see it. So we are using Spring Boot 3.1 point zero in Java 17. You can see that even without making any configuration regarding a servlet container like Tomcat for example, it has already started
with Tomcat, it has already started our application on Tomcat port of focus 80 80. So if we were using, for example, the Spring Framework in the traditional way, without using Spring Boot for this start and the creation of this application, we would have, for example, to install, prepare a whole container server like the banquet, for example. We would have to package our entire WAR-type application, for example, upload this application within Tomcat and only then would we start it, then we would be able to upload this application. After many configurations, then really with Spring Boot we
have all this ease and also this speed in starting an application. Of course, as we're doing here, step by step, piece by piece, we're taking a little longer, because I'm bringing up a lot of detail of this whole stage and everything that's going on. But after you already have this practice on a daily basis, when you already have this knowledge, you do this, this whole process really quickly. So now that we've started the application, let's go to our TG admin, let's refresh the table here and check it out. So now we even have products here,
right. Let's take a look here to see how the properties of this one are. Products Here we have the product columns of the edit name value type, everything is right, our main key and among other parameters and settings that we can see in this interface, right So our table was created successfully and then we come back here to visualize the persistences that we are doing there. After we implement our post method to receive these resources and save them to this database, okay? So, now we have our entity ready and mapped to the database. The next
step is to start our post here, because it is from this interface that we are going to use from JPR that we will already have several ready-made methods, without having to implement each one of them to, for example, save a certain resource, obtain a list of resources, a certain resource through its AI to delete a resource, update resources, among other things that we are going to see now in practice that you are already behind. As this facility is also supposed to be divided, all right, I'm going to create a new specific package here for us
to place our repositories, right. So you're going to stick with the Spring Boot example. When we post it, it will stay here and this package and inside this package we will start an interface, so let's select here in the idea and Java Class. But down here we select interface Set and this interface will be called Product Repository, right ? Product Model Product Proposal, then Product Controller, and so on. Let's start this interface here and first of all, let's extend the J Pear Repository. Why Because that way, extending the is already the Repository. Then we are
going to enable this power, this interface, this product like Repository. Why are we going to enable all these ready-made methods I mentioned earlier. For this we need to answer an already extend the JPH and post, and inside here we need to find what is the entity, what is the way that this repository will contemplate, in this case our product model. Let's import And what is the type of identifier it uses, which in this case is our aide Done that, done all the necessary imports. Another thing that I will leave here so that the Bean is
suggestive in relation to the extreme Bean where Spring, it has some stereotypes where we can use annotations, for example, to define that a certain class is a bind, that it will do all this control, all this management, so that we can then use inversion of control with dependency injection. So, as it is a class, an interface that we are creating, we need to somehow show the Spring Framework that such a class can be a Bean managed by it. Afterwards, if you want to know more about Bean, about the Bean's life cycle, about the types of
binding we have and also about producer methods, @, Bean, among many other things, you can access the ebook that is available here at description. And I'm also going to leave a link to a video where I went into more detail about these subjects. So, to make it more suggestive and quickly here to summarize Spring, it has four different stereotypes, in this case @ component. When we have a more generic class, we can use the @ component notation to show Spring that it will be a bind managed by it. And we also have some more specific
ones, such as classes of services that involve business rules. For example, we can use the @ service notation to show Spring that it will be one of the service type that will be managed by it. We also have the @Repository annotation that we can show to Spring, which is more of a stereotype than that class. That interface will be managed by him, the type that will have database transactions, for example. The same thing with @ controller, which is more of a stereotype than where we show Spring that it is a class that will be managed
by it, but there will be, for example, endpoints. Then it becomes more suggestive. Then when we use each of these stereotypes for each case. So, to make it more explicit here, I'll use the @ repository notation here. You can see that it is mysterious, like the Extreme Framework, to make even the Bean explicit that it will be a Bean managed by Spring, but in this case, which is a specific Bean, we could very Bean use this interface, create its injection points where necessary . _ _ _ _ But to make it explicit here and also
to be an example for you and also to have an example of each of these stereotypes in Spring, let's leave here the annotation @ will post right Just out of curiosity. That done, now we have our repository ready and we will be able to use those methods that I mentioned earlier when necessary, that is, when we are going to implement our complete CRUD in the controller. Once this is done, then the next step is to initialize our controller so that it is also divided correctly. I'm going to create the controllers package here and inside my
java class Product Controller. It will be a same class and Product Controller. We will also use one of these Spring stereotypes, in the case @ controller or @ watch Controller, which is specific for when we are going to implement a @ rest api. This controller is derived from the @control annotation, so we can use the rest controller to make it even more suggestive that this is a good Spring that has a REST API implementation, right ? that we just created, we are going to create a Repository product injection point here as well. We have here
some ways to start this injection point, for example, we can use via constructors, we can use the @ alter line of two or use it here from @ author, but for anyone who also wants to. Then we can even do a test using it, replacing @ authority with the constructor, right. So let's start our Product Repository here, which is an injection point for this interface, so that we have access to all these JPA methods when necessary . Well, now that we have our Product Controller, we can start to implement CRUD methods, methods, for example, to
save a certain product, methods also for reading, to list certain products or a specific product to delete a product and also to update a certain product. So the first method we're going to start with will be the post to receive this resource, perform an initial validation and save it in the database. But first of all, for us to receive these values ​​and addition, we will need to map these fields and for that we could, for example, use the model itself to receive and do this JSON conversion and receive it in an object Java. But we
are also going to use a thread that is related to Java 16 that was officially inserted, which are the resources that this possibility of Bean easily initiates these objects to transfer data such as DT, for example. So let's create a Bethel now to receive data from our resource. Products do the initial validation. If everything is right, then we convert to the module and save this resource in the database. So let's use resources to create this DT, that is, data Transfer Object and these data transfer objects in Java. So here in the same way I'm going
to create a new package for our DT and SDT. It will be called to become a suggestive Bean that we are using for records, it will be called Product Walker DT, then Nil, Java, Cless. I'm going to put the name Product Record DT here, but here we're not going to use Cless, we're going to select the record cast, a special Java type. So let's now start this record inside our dt oz package and you can see that instead of public class, now we have a new Java reserved word which is Record public Record, Product Record,
DT and in here, in here From these parentheses, we are going to start all the product attributes that we are going to use as an argument. So, in our way, we have the IDE, the Bean and the beauty. Just remembering that we don't need to worry about the IDE, because it will be generated automatically. So what the customer will send to be saved in the database, he will send the name of the product and also the value. So let's receive these two attributes here in this DT, both the name and the value. So here we
just need to start the type of attribute and also what is the name of this attribute, in this case Extremo name and leave it in the old one and the record later. I'm even going to leave here a link to a video that I brought here on the channel, where I explored more about this new feature of Java. It already has several methods ready, such as the get methods, three count methods, a string and with the rest with the constructor too, for us to start a certain record now. And remembering that React are immutable, that
is, once created we can no longer change their values, right ? We also don't need to define that these attributes are present, because by default they are already private and of the parent type, not the final modifier. And now here to include the initial validation, let's insert some validation annotations, such as the @ Note Blend. So the name of a product cannot be blank or an empty string for example. So let's use @ not blend and the value will be @ @ nati. So we will not be able to receive null values ​​to be saved
in the database. You can see that above, both the Note blend and the note do not use the Jakarta Validation package, which is the former JavaFX Validation. So there was this change in the naming of packages in Spring Boot. So now we use Jakarta Validation Constraints and then @Nathy @, blank, @ sage, among many, many other annotations that we can use for these validations, right And now that we have our product Record DT let's start implementing our post method inside our Product Controller to then receive all these attributes to be saved in the database. So
here, in order not to get a very tiring video, I'm going to paste these methods, because otherwise I'm going to spend a lot of time typing and I 'd rather paste and comment each line by line, each of the parameters of the definitions that we're making, right So here this is the first method, our post, right I put the name as the product key, it will be the http post method here. First of all, I'm going to do all the imports so that everything doesn't turn red and keep accusing that we need to do these
imports. And now we are going to comment on each of these annotations, each of these attributes, these properties that are being used to implement this post method Let's expand here, ok So, going back, we are creating a Westwood API following Leonard Richardson's maturity model, where he defined four levels for a person to be considered a master. The first level is that even it uses the HTTP protocol according to the level, which in this case is level zero and that it uses the HTTP protocol and level one and that it has Bean resources defined and the
correct one, the data and root. That is, when we are going to define each of the warnings of these resources, we need to use, for example, nouns so that it stays within the standards and that it contemplates each of these levels proposed in this maturity model. So you can see that we are using the HTTP protocol, we are already using each of its methods that make up another level of this maturity model. So, here, for example, when we are going to define each of these resources, let's build our URI. We are already using the best
practice of a noun to refer to this feature. Here we will receive here in this API method. Well, the other level of maturity is level two of the model and we use HTTP methods semantically. That is, when we are going to use a method to save a certain resource, we need to use a post that we only want to list these resources for reading. We have to use the appropriate method for this, which is the GET method. If we want to delete a resource, there is a specific method for that, which is the delete method
to update the put method and so on. So we are going to semantically use the HTTP methods and also have the appropriate returns for each of the cases and at the end, we are going to also implement Rich which are these hypermedia media that allow navigability between the API resources, but for now here we are already using of the HTTP protocol. We are already here defining resources in the correct way, semantically using HTTP methods as well. So we are practically already satisfying three of the levels of Wish's maturity model. Well, so here on line 21,
I'm using @ Post map, where I'm passing the URL. When we receive a post from the client. This method will be triggered to respond to this request. And now here on line 22 we are going to start this method. Finally, public. This one will be the return of this method, in this case, the points before, the type of product model that we are going to return and this mode that will be created and saved in the database. The method is called Share Product and this method what it receives, it will receive as the body of
the http request via post, it will receive it as the body, that's why we have to annotate our content product request here with @ wicket bare. And here, we already mapped this data previously and what this graph contemplates. It will then receive the name and value through Java objects and for this validation that we did earlier, it really takes effect, we need to include it here in the at the beginning of the method the green @ annotation, because if we just put these validation annotations here in our request Depot and don't put this green @,
no validation will be done and we won't have the expected result. So we need to remember to annotate our product request dt with the @ green so that this validation is actually done and our @ validate is also part of Jakarta Validation, right Ok, so moving on here, what we are going to do initially, When we receive an certain DT clipping First of all, let's start a product Model, because in fact we are going to save the model in the database, not the DT. It's just there for us to receive who is coming in the
body of the request. It's just us doing this serialization to a Java object, doing these initial validations. But after doing all this initial step-by-step, we will need to transform and convert our DT to model so that our model is saved in the database. So here I'm going to start with the mode term. I'm already using the VAR here, which was also inserted in Java ten, where instead of having to declare the type ten instance of this object on both sides, using the VAR within a closed scope, we can make this definition only on the right
side and not on the right and left side. We manage to save a little bit of code there, following, starting the mode side there and having already received our product Rule of DT from the client. The next step on line 24 is to do this conversion from DT to model and for that we can use Spring's own resource, which is the COP Properties point, which is this method that it receives, what will be converted and the type that it will be converted there to perform this conversion itself. Then he receives the Product Echo from DT
that will be converted into a Product Model and this conversion is done on line 24. Of course, there are many other types of conversion, such as Model Mail, Object Mapper, among others, but in simpler cases like this , we can quickly and easily make use of this Spring feature. After this conversion, on line 24, here on line 25, I'm already building the return. Remember that our method returns a space between being of the product model type. So here we start the space itself, where it is built in two parts, first and state. That is, if
everything happened to Bean, we also have to use returns consistent with each case, also to satisfy Richardson's maturity. So, as we are creating a resource in the database, we need to send the client that this resource was created. So we use HTTP, the idea that it is 201 as the status is in the body of this response, we can send what was saved, that is, the name, the value that the customer entered and also the ID that will be generated when this save is done in the postgres database, it's like we are going to do
all this saving of this resource, since we are using the product repository Remember that I told you that we have to allow us with an easy method adapt without us having to create each of these methods and we can use them in a simple Bean way. For example, here we had already made the injection point. Look, we have a range of methods here that we can use. Find you all inclusive, use sort, pagination, usual find by, bullet key and many others. And what we're going to use now is method six, So Product Repository point six
is ​​Here inside we pass the product so that it has already been converted and its fields have already been populated with the values ​​received here from the DT. And the ID will be generated when it is carried out, so this transaction with the database, right . The application has already gone up. For that I'm going to use Postman, so I've already prepared the post here and we're here using the post a URL method, we have to pass the complete URL, in this case http localhost 80 80, which is the Tomcat fire port that the ours
is spring. It was started bar products that this URI that we just defined there for the right post method. Once this is done in our bar, I have to pass our JSON. The two attributes we're working on, both the name and the value. And now we are going to send this request to our application, which I just started. So here, look, it has already started the Save Product method, it has already been triggered there and it will start to answer, So let's go through each of these points here. First I received the Product Record in
here, you can see that I already received it using our record, that is, our Java object. The monitor name gave 27 that I put here. It's a value and now I've started our model. For now it is empty, both the IDE in value, everything is empty, and now using line 24 we are going to use the bin first to make this conversion from DT to model. That done, now, before saving, let's take a look here at how our state is in this way Look, so it already has the name values ​​and the value of the
product and for now the product index is in the why it's still going to be generated, right? And now let's finalize this flow. I'll come back here in the Time post and look at the return 201 CREATED Ok, so our resource was successfully created, the IDE was also generated, everything was correct, the name and the value, so everything is ok. Now let's just do one more little test, I'm going to try to send this request with the product name in an empty string. Let's see if we're going to receive our bad request according to the
validations that we've inserted previously, since the name is not black, it cannot be null, it cannot be an empty string. Let's see how this return will be. Just look. Bad request, right So we couldn't save it, It's the same thing. Now I'm going to put a certain name and then we put it as the value. I'm going to put LG washing machine here , change the value here, in this case value, let's put it in let 's try to send b bad request because we need to pass a correct value here so that we can
really save. So our validation that we did earlier on our record is working as expected. Let's put three 500 here and now let's try to save this new product. Now yes, the validation was done correctly. We receive here at the Product recognized even the name and value. Here we start the product in I mode, we do the conversion, we save the product and we receive the washer sample here with yours. The specific ID, right So that's it! Our post method is already ready and now to continue our CRUD, we are going to implement the reading
methods, both the GET method or to list the list of products and also the GET the year reminding the staff that regarding the bad request for specific messages appear so that the client knows what he did wrong, what is the field he needs that is not being sent. We can implement many improvements, such as customizing messages, customizing errors, inserting a Custom Validation too, configuring the Exception render among many other things that remain there as improvements and then as complements for you to implement in the API. Now moving on here, let's go to the next method
which is going to be first the GET method or this GET method. It will list all the products that we have saved in the base. So, first of all, it will be the GET method, which is the reading method for us to follow the semantics of the HTTP methods. Work the GET method. It will also trigger with wall and slash product public. Its return will be a response. So, if, however, in the body it will have a list of products, a list of product module, then here we will import the list do. There, this method
will be called Get to the Product, it has no input and it will return the aware spy with the HTTP status, ok Status 200 is the body of this response. It will have a list of all the resources that are saved in the database and for that we use it in the Product Repository. End point ok. And this method is also ready. In the JPG they already select all the resources saved in the right base and so we will already have the implementation of the GET method. Ok I'm going here again for the application, let's
upload the application and test this new method in the post. The application has already been uploaded, I already left it mapped here in the main post as well. Method get Tempo We have to select here the correct method Method Get Louca Run 70 80 Barra product. Let's send the request here and it's only 200, ok And it listed the two products that I have saved in the database and the next one. Now the next step is for us to also implement another reading method, but now it will list all the resources, but a single specific
resource, because in addition to asking for all the products in the Product all bar, then I will also pass the ID of which product I want to view. So I'm going to call this method Get Wan, that is, it's going to get just one resource, it's going to be here below the GET method or else here some annotations that we haven't used before, but that I'm going to pass on here all step by step. So what's different about the GET method to the product This method get the product. It also uses the method get http
@ get map and here I defined the URL it will call and then let's say like this, composed of slash products slash, the index, in our case, is the ID of the product that I want to view. So it will select based on a single product, passing the specific ID that is coming here in the URI and the public method the response. Before here is a little different. I put it as an object because we're going to have two types of return responses, depending on some different situations. Here, inside the Get Wan Product method, we
use the @ pet variable annotation so that we can get this ID that is coming here on the web. So we use the Pet Variable, pass what is the correct name that is here in the URL then inside the fleece and we have to pass exactly what we defined in Hurry so that it can get this value. And here our ID is like ID. Once that's done, what are we going to do ? it will then do a select on the base, passing the WHERE clause when the ID is equal to such and so if
it has any return, that is, if there is that resource with that ADD, we will receive the product here or as the Optional, which it's one of those Java types, right where we can work with some ready-made methods. So, for example, let's do this search in the base of it will say if the product or it exists it has a value and return this value from the base, we will do a simple check here Bean using from Optional product to the point except, that is , this option is empty, if not, there was no return
from the base. Let's build an answer for this that will be, but patience and period. HTTP status status not found. And in the body of the response we will put a message here that the product was not found, but if we have a return from the database, then this IF will not be satisfied and we will then build this second return. Are you aware of the HTTP status point, ok The status 200 is in the body of this response to pass the product here and as we are using it receiving an optional base, we need
to use the GET point here for us to return this model product to the client, right So now our Get o and product is ready reminding the staff that later to do other dealings, we can use classes, validations and customizations for that. For now, let's do it this way, since we are dealing with returns, so nothing more coherent than making these returns. These construction of these returns here within the controller itself. I'll go here to the application and upload it again, just so we can test this method. Get an and, which is also a read
method. The application has already been uploaded and here I'm going to take one and I'm going to take the one at the bottom here. One of those says that we already saved it previously and now in this new mapping of the GET method, when we pass here the complete URL bar products bar the ID that we want to get, I'll pass it here and send the request, ok It just returned the data of this specific resource, ok Then we will delete this resource and we will try to visualize this resource and we will see that
it will return, that it does not exist. For now it's working as expected and now we're going to continue with the implementation of our REST API, our complete CRUD. So we already have here methods of creation and reading. Now the next method is the update method, the update. So let's create this method here in our controller, the Put Update Product method. It is very similar to the one we just implemented, which is the get o and product. First, because in Hurry we need to go beyond the bar products, we also need to define passing what
is the ID of that resource that we want to update, because this thing doesn't exist, we don't even have what to do. He's already going to do some initial research there. If this resource does not exist, we just return it to the customer who cannot update it, because the product he wants to update does not exist in the base. Let's use the @ put map here. Semantically with http there will also be a return. The response before is of type Object, with different returns for different situations and our update product. So here it will receive
the profile and the ID that is coming from the URI and it will also receive the body of the request. In the case of Request Bare, which will receive the name and value parameters there to be updated. For example, I want to update the name of a product, the value of that product, so I'll also pass these fields in our product record. DT Remembering that for the validation to also happen here in this case we also need to use @ vet, right Having done that, what are we going to do at the beginning of this
method First of all more money is coming, so before updating I better go to the base of data. See this feature. Exists If this resource doesn't exist, we don't even need to make any transaction, we'll just put together the same response we did there previously and send the client an http not found status saying that this product doesn't exist. But if this product exists, then what do we need to do? We will need to update the fields that are, that is coming, that we are receiving from the client for this. Remembering that here on line
47, we have already searched for this product in the database, so if it exists, it will not enter this ICE and will come here to line 51. In this case, we will also start a fashion product to be updated in the database. But we are not going to start a fashion product, an instance from scratch. We will assign the value that we have already received from the database. Because the ID of this resource is already populated, it already exists. So we can't start a new model product. We need to consider the ID that already exists,
so that we just change the fields there like name and value. And the ID remains the same, right So that's why here we're going to start our Model product and here I'm going to use the Product Optional, the GET point that we've already searched for before, the result was coming from the select in the base of data. Once that's done, follow what we've already done in the Post method. We need to do it the conversion of the updated fields of, our clipping, the conversion of the DT to our product mode and after that we will
save this product using it also in the save, but remembering, passing the same ID so that another resource is not created, but just updated that resource that already exists. And in response, we'll also set up the resulting states, ok And in the core, we'll send the result of that database transaction with these fields already updated. That done, guys, let's go here for the application. I'm going to put a breakpoint here just for us to visualize and now here in the post I'm going to get this index that belongs to our washing machine here in the
update, so here in the post I've already mapped or put everything right, then with the index the push method is already selected and now in the body we will also need to pass both a name and a value. So whatever name was LG washer. First let's try to see if our validation is working. I will try to submit, update from this feature with the empty name Bad Request. I couldn't, so the validation is ok now, so I'll put it here. Updated LG washing machine Let's change the price here, put a 1500 promotion. Let's send it
now to see if this feature will be saved. So let's see. Searched the database to see if this resource actually exists in relation to this. ID So the structure of the Optional, look, it comes inside the value there. That's why we always have to use the GET point to get the value of the option. So there is. It already has the ID, name, value, everything right, so it won't enter this IF, it will drop down here. Let's start our product separately, so that it will still have these old values ​​and now let's convert it so
that it includes the new values. Washer of and updated 1500, right Let's finish the flow here, come back here in the post and look, I got the now updated data of this resource. Now if we use, for example, Gateau, look, it already brings me this updated get to get year data, because it goes to the base and it already has these updated values, everything is almost ready. Now we just need to implement our delete method so that we can finalize our CRUD, given that it is the method referring to the deletion of a certain resource.
Remembering folks that afterwards it is as an improvement to insert implementing customization of message errors also to put another layer like the Service class, for example. Here you can see that we are using the Repository within our controller. It's a good practice, especially when a business rule is being inserted and that we use our service to intermediate these two layers, right ? And now that the delete method is at the end, then its structure will be very similar to the others, right First it has a specific HTTP annotation for it which is the @ delete
map, according to the semantics, the URL will be the same as the update methods and also the get wan method. Because we're going to have to pass on the ID that will be deleted. What is the resource that will be deleted . So the first thing we're going to do is search the base to see if this product exists to be deleted, because if it doesn't exist, we're going to send the customer that this product no longer exists and it exists there, what can we do Using delete from JPA itself So here you give a
space to show you when I put a period delete I can pass the whole entity here, I can also use it in certain cases, when there is a need for or also delete, it goes among other methods here ready, J Peak we have to be used when necessary, so I will use the Delete method here and here inside we pass. What is going to be this entity that I just fetched from the database as optional That's why I need to use the GET point to pass this product model there to be deleted. Once this is
done, we will return here the status and status and body of the response, saying that the product was successfully deleted again. Here I go to the application. Let's upload the application with these new changes and test this method in our Postman. So, as I did with the others, I already mapped the Delete method here in the post, so the complete Hurry is already here, I selected delete here and now here I just need to pass what will be the ID of the resource that I want to delete So here I go send the feature to
the washer I just updated, so let's delete this feature product successfully deleted. Now let's imagine that I want to delete it and try to delete it again, but it no longer exists in the base because it has already been deleted. So in this case I have another response which is the 404 not found product does not exist. Now if I try to update this resource I will get the same return. 404 It doesn't exist, so that's why I can't update a resource that doesn't exist. And if I also try to view the details of this
resource, I will also get the response 404 that this product does not exist and now our list will only have one product, a resource saved in the database, because the other one that we had called I just deleted. Okay, so I'll stop here now. Application is now. We have already implemented many of Richard's maturity levels so that our API is considered RESTful and the last level, which is the hypermedia level, where it is not so frequent that we see this mainly in the day to day in the market. But according to Richardson's maturity model, for
an API to be considered the rest, it must present these hypermedia, this navigability between resources. And of course, on a daily basis, we have to have a Bean API documented as essential and also reactions in many cases, depending on the complexity of the AD, it will present many limitations, but it is one of the pillars for an API to be really considered. If we are going to take the rest literally, then it is one of the points that we have to implement. So let's implement it from now on, which is also here as an example.
It is also at the level of curiosity and also as studies when there is a need for implementations. So now for us to make this API and use it as a ratio, to have these hypermedia, this navigability between the. We're going to use a spring project for this specific thing, which is the Spring Ratios that I even showed you earlier there at the beginning of the video and for us to then make use of these classes and methods, we'll need to insert here in the for an XML point, plus a dependency, in the case below
the web dependency. I'm going to add the Spring Boot dependency and starter ratios here and I'm going to add the month for the necessary download. Here in the corner, we can check if he has already contemplated, has already done that Look. Spring Boot Starter ratios 3.1 point zero So the month has already been downloaded and now we will be able to use the classes and methods to build these hyperlinks in our REST API. So first of all, I'm going to close here some classes that we will no longer use. I'm going to leave our fashion
product open here. So one of the classes that we can use in relation to RADIUS, we will need to implement it within our fashion. Why Because in the controller methods, in our CRUD what we are returning to the client here when we have these reading methods, both gateau and get wan, we are returning our product mode. So for it to have this navigability, we will need to implement reactions in our product correctly. So for that we are going to use the Web Presentation Model, which is related to widgets. So let's extend this class so here
before implementing it, let's here extends Representation Model. You can do the import and here we need to pass. What is this entity that will have this representation that is our product right? And here, you can see that the import of presentation model was done in the Spring Framework Radius package, which is this dependency that we just inserted. So that's done, now when we add to use our mode internally, it will already have some methods, such as the method, for example, for us to use to build these links. So we are going to make use of
some methods of this class that allow us to create these links for navigability, which is the first method that we are going to change. The return will be in relation to the get or product method, that is, now in our API, when a certain client requests the product listing from our application, it will come. The name and value attributes, but there will also be another fourth attribute that will be the links, that is, it will. We're going to set up this link that will redirect the customer to the details of that product, which is nothing
more than our method get the year product that he will then be able to analyze and be redirected to Hurry, which specifies a single product according to the your index. So that's what we're going to do here inside the Get method to the Product. Now we are going to change its structure a little bit. First, first of all, we were already doing all this searching in the database, within the construction of the conscious and in the return itself within the bug. We already activated the repository here. Full stop. And now, since we're going to have
to manipulate some values, John. let's do it all separately. What do you mean? First, let's start a list up here where all this search in the database will be done initially and we will receive in the product list the return of this query in the database, in this case products, repositories. Full stop. John, the complete list of all products saved in the database. That done, now let's make an initial check if the list is not empty, if it has some elements, we will have to go through each of these elements, then building this link. So,
for that, we will first use an IF to check if the list is empty. And second, let's use the structure for right. So here below the list, let's build this IF in which it starts and closes here on line 38 it starts on line 33 and closes on line 38. So, first of all, we're going to do a check to see if this list is empty. If it is not empty. I put a negation here Let's enter this IF and inside here we'll go through this for and in this for from the list of products
of type Product Model Product Model for each of these products, what we're going to do First we're going to get the ID of this product and receive he. As it is of the ID type, I created the ID variable here and it is here in the sequence. So let's use the point product and that is the method that I mentioned earlier that we have access to now, since we inserted the Presentation Model, since we extended the Representation model mode in our model product, so now we can use the Point Head method to build this link
here. Let's do the imports first, but first of all guys, I'm going to come up here with these imports, sometimes they fail here and for some bug in the idea so sometimes we have to put them here in hand when it doesn't correctly recognize which ones these are the imports that must be made for these two methods, both the uncle and the master link. Well, so here I put it in my hand. Remembering that later you have access to all this code, both in the ebook and in the Ruby video, which I will also leave
the link here in the description and now it recognized it below normally. So what we are doing Creating this link Each of the attributes of this list, of the products on this list and for that we use the to link, that is, to which endpoint, to which method that I will redirect the customer when he clicks in this link and what is the method on, that is, what is the controller that this method is and what is this method itself that will receive this redirection So if he is in a list of products and he
wants to see the details, a single product, we have to direct it to, in this case, the specific product which is the get o and product method. So, here within methods where we first define which controller this method is in, in this case our Product Controller, Ponto Class and here at the front the method name that we have down here, which is get o and product, right And here, like this method, it receives an ID, we need to pass this ID that we are receiving here from each of these products that are being entered by
sort. And at the end, here we use id self help, which is the method it will show. He will then be redirected to each of your products. Anyway, this is also where I did it separately to make it more suggestive, easy to visualize according to the didactics, but all this construction in the case of getting the product id point, we could do all of this in here. From here I just made it separate to make it visually easier to see what's going on. That's what I'm going to do here and at the end we're going
to build the answer. What's aware of the points, that's ok. And in the body of the response, we will pass the list of products that now, if it is not empty, it will have the construction of each of the links for each of the resources. Now I'm going to upload the application and we're going to make this call to see if we're going to get the expected result. Let's go here No Postman post Check it out. Before this was the result for a single resource these three attributes. And now look, we have a fourth attribute
here that is not the link, so it shows that it's the link to the resource itself. The details of this resource is the HS, it shows all the URL that I can be redirected to see me, so these details of this resource, of this product, right If we put one more resource here, I'll save the washing machine again, just for us to have two, two resources here on the list. Just look. Then it will assemble this URL each of these resources until the end. You can see that it has the product specific ID. What is
the final 515 here the second product final nine nine final nine nine in Hurry. So that's ok And now, just to be coherent, let's implement the opposite in the Get o one product method, that is, when I'm requesting the details of a certain resource, instead of having only the three attributes, it will also show me a link. If I want to browse to see the list of all products for example. So, inside our Get Wan method, we're going to make just a small change. In this case, we are going to use the same ED method
that we used earlier here Product here. Before sending the response to the client, let's build the link. But now instead of referencing the gateway, let's refer to the GET all product method sure that it will be redirected from a specific product to the listing of all products. Again, let's upload the application with this new change. And now let's say here in the post I 'm going to get the index of one of my resources here at Get Wan. Here before we had only three attributes and now sending the request we have one more attribute of
that links where we are seeing the details of a certain resource. But now he relies on this link to navigate to the full list of all resources, ie all products. We have a RESTful API according to the maturity model defined by Leonard Richardson, right But then guys, now here are some tips and suggestions also to continue the studies in the search for knowledge would also be to improve the API, including pagination in the methods. Here listing all products. Also include filters to make this listing more dynamic for the client and also insert error customizations and
also insert layer seven as new business rules appear and also place the boss layer, create Beans of the type @, set and among other things . And that if you are also going to work with dates, remember to maintain the standardization of dates. But remembering that many of these concepts, both after Richard's maturity are the REST architecture briefing constraints. Many of the concepts and definitions about Spring, both Spring Boot and Spring Framework Binding, among many other things. You will find the step by step in the e-book that is the link here in the description and
only you. This free ebook that already has the updated version 2023 contemplating these new versions of Spring, Spring Boot three, Spring Framework 6 and also Java 17 I will also leave here the link to a video that I already brought here on the channel, which is the with some of these suggestions here, such as standardizing dates, pagination, among other things. For those who are curious, also want to go deeper into this subject. And that's it, I hope you liked it. Now, after all this implementation, we built a REST API step by step. We then implemented
all of Richardson's maturity levels and I hope you enjoyed it. Don't forget to leave the thumbs up here so that this video can be shown to other people who are also wanting to learn more about Spring about Java about APIs And also don't forget to subscribe here to the channel to always be notified and receive all the contents. I'm posting over here, right See you in the next videos here on the channel. Until later!
Copyright © 2024. Made with ♥ in London by YTScribe.com