Developing RESTful Java Web Services using Spring boot | Vinod Kumar | Skillshare

Playback Speed


  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

Developing RESTful Java Web Services using Spring boot

teacher avatar Vinod Kumar, Software trainer, developer & consultant

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

16 Lessons (1h 18m)
    • 1. Introduction

      1:22
    • 2. Getting started

      9:30
    • 3. Customer Rest Controller

      9:23
    • 4. Customers data for our application

      3:00
    • 5. Setting up H2 DB with data

      3:55
    • 6. Mapping Customer class to customers table

      5:06
    • 7. Getting data from DB in our rest controller

      7:21
    • 8. Adding pagination

      6:01
    • 9. Postman for making requests

      2:26
    • 10. Get one customer

      5:46
    • 11. Adding new customer

      4:11
    • 12. Update customer data

      3:24
    • 13. Delete a customer data

      2:52
    • 14. Swagger for API Documentation

      8:39
    • 15. Building distributable bundle

      4:08
    • 16. Thank you

      0:30
  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

136

Students

--

Projects

About This Class

This course enables a regular Java developer with the power and simplicity of Spring boot applications. Via this course, you will understand the basic concepts of RESTful web services and how Spring boot simplifies the development of a standalone, self contained web service using which you can quickly develop and deploy production ready Java micro services. For taking this course, you DO NOT need to have prior knowledge of Spring framework.

Here is what you can read about Spring boot and a basic comparison of Spring framework and Spring boot.

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".

We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

Features

  • Create stand-alone Spring applications

  • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)

  • Provide opinionated 'starter' dependencies to simplify your build configuration

  • Automatically configure Spring and 3rd party libraries whenever possible

  • Provide production-ready features such as metrics, health checks and externalized configuration

  • Absolutely no code generation and no requirement for XML configuration

What is the difference between Spring Boot and the Spring framework?

  • Spring is a light weight and open source framework created by Rod Johnson in 2003. Spring is a complete and a modular framework, i mean spring framework can be used for all layer implementations for a real time application or spring can be used for the development of particular layer of a real time application unlike struts [ only for front end related ] and hibernate [ only for database related ], but with spring we can develop all layers

  • Spring framework is said to be a non-invasive means it doesn’t force a programmer to extend or implement their class from any predefined class or interface given by Spring API, in struts we used to extend Action Class right that’s why struts is said to be invasive

  • In case of struts framework, it will forces the programmer that, the programmer class must extend from the base class provided by struts API

  • Spring is light weight framework because of its POJO model

  • Spring Framework made J2EE application development little easier, by introducing POJO model

Spring having this much of demand because of the following 3 reasons….

  • Simplicity

  • Testability

  • Loose Coupling

Spring boot:

first of all Spring Boot is not a framework, it is a way to ease to create stand-alone application with minimal or zero configurations. It is approach to develop spring based application with very less configuration. It provides defaults for code and annotation configuration to quick start new spring projects within no time. Spring Boot leverages existing spring projects as well as Third party projects to develop production ready applications. It provides a set of Starter Pom’s or gradle build files which one can use to add required dependencies and also facilitate auto configuration.

Spring Boot automatically configures required classes depending on the libraries on its classpath. Suppose your application want to interact with DB, if there are Spring Data libraries on class path then it automatically sets up connection to DB along with the Data Source class.

  • It is very easy to develop Spring Based applications with Java or Groovy.

  • Spring Boot reduces lots of development time and increases productivity.

  • It avoids writing lots of boilerplate Code, Annotations and XML Configuration.

  • It is very easy to integrate Spring Boot Application with its Spring Ecosystem like Spring JDBC, Spring ORM, Spring Data, Spring Security etc.

  • Spring Boot follows “Opinionated Defaults Configuration” Approach to reduce Developer effort

  • It provides Embedded HTTP servers like Tomcat, Jetty etc. to develop and test our web applications very easily.

  • It provides CLI (Command Line Interface) tool to develop and test Spring Boot (Java or Groovy) Applications from command prompt very easily and quickly.

  • Spring Boot provides lots of plugins to develop and test Spring Boot Applications very easily using Build Tools like Maven and Gradle

  • It provides lots of plugins to work with embedded and in-memory Databases very easily.

Meet Your Teacher

Teacher Profile Image

Vinod Kumar

Software trainer, developer & consultant

Teacher

Hi, my name is Vinod, and I am a software trainer, consultant and freelance developer. I am also the creator some of the highest rated courses on Udemy and SkillShare. In my professional teaching career (spanning over 23 years), I have trained hundreds of thousands of software engineers. Teaching is not only my profession, but also a passion. Creating online courses gives me the opportunity to reach and help students across the world.

See full profile

Class Ratings

Expectations Met?
  • Exceeded!
    0%
  • Yes
    0%
  • Somewhat
    0%
  • Not really
    0%
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

1. Introduction: hello and welcome toe developing. Rest full Web services using springboard. My name is we know, and I have over 24 years of teaching experience in 90 industry. In this course, I will show you how easy it is to develop practical production. Really rest ful micro services using springboard step by step A springboard is the springs convention or configuration solution for creating stand alone production grade spring based applications that you can just run. It is pre configured with the spring teams opinion interview off the best configuration and use off spring platform on third party libraries such as Apache Tom. Get so you can get started with minimum fuss. Most of springboard applications need very little are no spring configuration at all. Even if you do not have any prior knowledge offspring, I assure you that you will be able to understand on you spring boat for your projects. All I expect from you is fundamental knowledge off Java programming language. Before you start quoting along with me, make sure that you have Java Wonder eight or later Russian install, and you have downloaded the eclipse idee for into placed our developers. So let's get started 2. Getting started: The first thing that we will do is tow with it. Start Springboro your weapons which takes us toe spring initialize er application. Here we can configure our project such as Do we want to use Maimon Project or a cradle project? I would like to use May when Posit you can court spring Good using job Ocotlan are groovy. I would choose Java in the spring ball. Washington salvages stood out one about seven and you can give here your project Details such as? What is the group? Buy the artifact Charity. Let me give it us court, are we not? And spring boat Devil. There are a few options that you can explore such as What is the description? Was the package name going to be? Is it a job project or a war? Most of the time, we would be using springboard for creating micro services or where baby s. So it's gonna be tested. Jar find standard on project the Washington jama. I would leave it to the people, which is eight at this time. And then you can add some dependence is by either typing here if you know what you're looking for, such as Web security JP actuator, Deftones etcetera. As indicated over here are you can click on the list here and chose some off there for this project. I will only add one single dependency on as and when we need, we will are more so I'm just like, well, and select the spring Web starter at this to our list. Now we condone look, the entire zip file consisting off the main one project all I can explore individual fights and choose whatever that I want. So I choose to go with this. Explored the project option that will show me the Ponder XML and any other related files. I simply have to click the copy button that copies the content off Ponder XML to our Clipper and then Al Gore a kids. I have only opened eclipse in a brand new workspace. As you can see, there is nothing over here. I will simply create a new May 1 project Check on this check mark that says Create a simple project. Next, I need a group idea Escort. Are we not artifact Early as spring boat? That's General. I leave the washing as 001 snap shirt and packaging US job Click finish To come out of this Here is the project that God created which doesn't have anything except a bomb dot xml I opened upon the wreck salmon Replace that with the wonderfully copied from spring initialize er as soon as the same, it eclipse will start downloading all the jars from the dependency list. This may take a while in case if you're doing this for the first time. In my case, it has already done because I have worked on this kind of examples earlier. So all the maven dependencies have come to my project right now. But you may see that there are a couple of errors over here that says Maimon configuration problem number one. It is unknown so we can ignore this. The 2nd 1 says Project configuration is not up to date with Ponder XML and select May 1 update project from the project Context Mental are used Quick fix All we have to do is tow kicked the project, go to May 1 a big project and said OK, and there's they're gone. Let's close this. Create a new gel a class that actor, that starting point for our application So I'm gonna right click on this SRC main job New to us. I'll get the package as quarter, you know, on the name off the class happens to be application. You can give any name you want. I just wanted to give application. The application is simply going to have one main function. And I'm gonna say your spring application Doctor on and then I supply the application Dark class itself. One last thing I have to do is tow Say that this is a springboard project by adding an impatient at the rate spring boot application. Save this. And now I can run the court when it on this program, spring will run an internal down catch server on blood support. Eijiro, eijiro. In my case, however, 8080 has been used by Jenkins. As you can see you, it has been used by Jenkins. So I have two options. Either stop Jenkins at Agia Eijiro. And although this spring application to run on 800 all I can ask spring to use a different port number. This can be done by adding a properties file to our resources section. So I'm gonna are here file by going toe. New other gender flight Let's name this as application start properties. Now these properties can be configured either using application that properties are application, or why am L or application or family fire for this demo? I will stick with application dot properties, and all I have to do is to say, Sir, we're not fourth equals Toe A pulled off my choice. Let's give us 7777 I saved this close this application that properties right click on this file, but on that's Joe application at center. They do that on the pencil. You can see that it comes up with lots off locks that includes logs from dumb. Get saying that Tom get started at Port number 777 Started application in 5.903 seconds, and as you can see that the application is still running, it is not quit. It's not dominated. I can forcefully terminate them over here. But if I want, I can go to a brother and access this local host Holland 7777 and then press enter and we get some white label error. But of course this is an output coming from our project that it hurts, um, greeting message over here instead of this era page. So let's go back to our core and ready function your core public string Say hello, which returns a hello world message. But if I want this particular function toe respond toe this brother request we have to add to annotations here number one We have to say that this the rest full controller by typing a marriage arrest controller And then we have toe other mapping to this particular function , saying that whenever the user makes a get request for local 777 we want to actually restaurant from here so that can be done by typing at marriage Gift mapping. Save this. No, not that the previously done program has to be stopped if I want to run this again. So I have stopped it. Right? Click on this program run as Gela application. A similar output comes just like the previous one. And now I can come back to the browser and refreshed the same and we should see here. Hello. World message came up. Now if I want to change the message, I can change this. But then again. I have to click the stop button and then run the program again. Now, since we want to develop Web services quickly, this can be a herd of So what we can do now is I can stop this for a minute and then other dependency from spring initialize er or as deaf tools. So all I have to do is tow at the dentals as dependencies, get the new Bondevik salmon, and then replace the current ponder XML with the new content. Save it and you will see that Eclipse is downloading the new abortion on its updated. Now, I can actually run this project once and then change the content whenever I want. And every time there is something changed and the file has been saved, the fight gets combine on automatically. Dentals will reload this once again. For example, instead of follower, I can say hello, string boot savage and you can see that there is a reloading off the server. I go back to the browser, refresh this once again and you'll see that it is hollow string boat again. 3. Customer Rest Controller: Now let's have a more useful rest controller here. Instead of doing everything in this application itself, it created a new class that acts as a customer rest controller. And this controller should have mapping for get post put delete requests. So first of all, let's create a new class here called as customer arrest controller. We can also add a sub package or here saying that this is something to do with controllers say finish and notice that every time I add or modify some fights, springboard project is gonna restart. So that can be sometimes annoying. I'm going to stop that. And here I am going to say that this is a rest controller as well as this is gonna handle all the request for a perfect called a p A customers. So you can do that by typing at very request mapping slash a B I slash customers, which means that I can come or here locals double seven double seven slash AP, I slash customers. And then when I send the request, this request will now be handled by one of the function within this particular class. And let's say we want to have a function here, which gives me some response. So I'm generating function here called Public. I can say stream. Let's call it as get all customers. Have you done all customers? Data will come from here. The only thing that I have to do here is to say a deal. It get mapping. Now, after it get mapping is equal in tow. A request mapping with a matter off. Get so I can as well say you request mapping Met hurt equals toe request method. Don't get so this was the only way you could have done in the past. But no, we have a simpler way of doing by saying get mapping. So let's say this or on the program and see if we can get this going. So I'm gonna go here right click run as shower application and then I can come down here and then say, a p a slash customers. I pressed Enter and it says all customers data will come here. But what we want is not a string but a Jason representation off customer data, and we don't have any class representing customers. So let's begin by creating a classical customer representing customer data for this. I'm gonna go and clear the new class called customer. A right click on this chord. Urbino dot controllers New class. But this class being an entity class, I can name the packages quarter, you know, dark entity or model our domain and then you the name as a customer. Let's assume that we do have a in teacher i D. For a customer. A couple of other details, such as customer mean and Eamon. Probably a phone number. Let's say City stayed on and probably country. We also want to know whether the customer is male are female, so I can say private string gender and I think this should be good enough for our customer class. Since all these are private variables, you can only make use off them by exposing while getters and setters. Traditionally, we would have done that by going to source generate getters insiders. And then you can add a two string constructor using fields. Constructor is in super class, etcetera, etcetera, but we do have a better mechanism called Lombok. Now to work with Lombok. Let's goto our spring initialize er add Lum book as a dependency. Get the new your washing off bomb. Can't be that and go back to our project. Replace the current ponder XML with that one. And in a minute that project is ready. Now, in my case, Lombok has been already set up. So if I goto customer java and adhere agday rate no arcs Constructor, you will see that there is a constructor in the Hope line as well as I can say that the rate getter at the rate setter and you can see that get us and said us are added automatically. Now, in your case, if you're using lumber for the first time, this will not happen. So first thing you do is tow Goto. Maybe Independence is expand this finder Where is the lumber jar? In my case, right here Rightly can say copy, qualified name. And this will give me the name and location off the job. You can also see it stages. But here it's under my users. We don't Come on. Don't m two repository our project. Lumber, Lumber. It's a lot so you can find it right here. Now, on windows, you have toe go to that location and double click the lumber. Torture on Mac are leaners open air terminal and then just job minus job followed by the jar. Finally, because this copied from eclipse and then when I pressed Enter it is gonna come up with the user interface on this user interface will search for on the ideas that are there like this . In my case, it is under slash applications eclipsed or app. If you don't see your eclipse location over here, you can kick on the specified location finder the eclipsed our I A file on. Then you should be able, tow, install or update. In my case, it's already updated. You can see that the symbol here. But if you want, you can always say install update and then quit the installer. Once you have installed, you have tow close eclipse and open. Now do not go file Restart. That doesn't help your physically close eclipse and then open again and then you should be able to see your respective get a sitters. Are any other additional annotations that you have used now that I have here a customer class, let's go to our customer rest controller and say that we would like toa return a list off customers over here. And to do so I can say List off customer import list from Java Util by pressing control, shift on Windows or command ship on Mac and make sure that you select from Java Util list. And now we need to return a list off customers Over here. Let's say I want to create to customers and then you're done them as a list. So it's a custom or C one equals toe new customers, If you wonder, said I d off one that is a name off that they have you my name C one dot said email. The wonder said for but and then see Wonder said City Bangle City Wonder said Gender. I can also create a second customer. Let's call it as C two closed toe new customer and then I'd say seafood are said idea off to settle Rcep Name off rotation, said email. Andi, I think see do DOT said City should do the job. Now, in order to return these to customers as a list, I'm going to simply say return. They start as list off. See one. Come on, C two. Now that dysfunction is ready, let's go on read on the application Dark Java, which starts over Tomcat server. And then I can go back, toe the browser and access the same thing and you'll see No, I got a Jason wash in off the object. 4. Customers data for our application: No. The big art data, which is hard coded. Let's get the data from databases. For this, we need two things. Number one, we need an RV VMS server and number two. We need data file so forth. Let's go to our website car Masaru dot com which gives me mark data. So here I can choose. What are the fields I want? What kind of data that I want for those fields. And I can choose between CSP Jason SQL etcetera. How many does he want up? 2000. You can get it for free of cost. You can also choose what kind of line ending and so on and so forth. So in our customer dot Java, we have i d as a number. So this is gonna be okay. Roll number. I don't have a first name. Last name, but I have just name so I can delete one off them and then call this as name but the kind of data I don't want only last name. I want the full name so I can look for full name. And that's gonna be full name. I want email. Gender. I don't want an I P address but I want a phone number. So I select the data like to be a phone, and that's gonna be a phone number. You can also say what format? Off the phone number. I just want 10 digit phone number. Nothing else. I can also rearrange them on another field within city. That's at a city. One more skit, Andi. One last four country. And then I can say here, Hominy Rose, I want 1000 is okay with me on one sq all format. What is the name of the table? I'm gonna call this s customers in good clear table also, and I can click on download data and I don't want the same thing in tow. My workspace location, which is under desktop on my course folder under workspace. I'm gonna save it as customers dot m s s. You know that the file is ready. Let's go and open the same thing in a text editor which is here. Customers are MSs you'll open with. I'm gonna open that with extend it on this. No, it looks like create table customers into idee noticed that there is no primary key. We can add them on DSI here primary key. Otto underscore. Increment name is supposed to be not none. Our email. Let's make it us unique. Sending with phone is gonna be unique. And the rest of them are okay with me. I'll save this and close the fight. 5. Setting up H2 DB with data: Now that we got the data, let's use an RGB Amos called hits two deliveries so you can go toe heads to rid of its dot com and then look for its download options. The car in question is wondered for that one double lane, which somehow seem to have some issues. So let's not download this one. But let's go toe all downloads and then you can see it is the last to stable version happens to be wonder for the 1 97 Now, if you're on Windows, you can download the Windows installer with simply installs a folder on your computer. Ultimately, you can download platform independent sit file, which I have already downloaded and unzipped in my home directory. So if you happen to see here under my home directory, I have a folder here called H two and the H toe has a BIN directory hits to dash wondered for 0.1 $97 jar. They got a such file that you can use in mac or leaners yourself hit student bad for windows Now on windows. You can simply double click this file, and you should see that the server will be started as well as a client application will open in a browser on Mac or leaners. You can just copy the part off the fight by going toe services, copy pot and then open it. Don't you know? And then based this press enter, you can see that the Sarwar started on on the menu bar. You consider the Nikon on Windows, you will see the similar icon on a notification area at the bottom. Right. The browser that came up is basically a client application that allows us to connect to this particular server. In case if you happen to close this, you can always get this back from this icon, then click on its to console. Now, if you're running this for the first time, you may see something called Generate Age toe embedded and the options would be a little bit different over here. So make sure that your selected generate cage to Sarah. Here you can give some custom name and click on the save button on this name will appear in the job. Done. The driver plus happens to be our door heads. Tudor Trevor. No change over there. The GBC Orel has the server and climb protocol the central location and then you have a name off the levy, which is called test by default. I can change this to a new DB car. Spring boat that's training. And then click the best connection to see if everything is OK. You may notice that the user name s a and password. A blank password is default. If you want, you can change that and then click the connect button on. Then you will be taken into the database. Now, here you can type in command such as create older drop, select etcetera. But since we already have got the data in an Eskil file, we have to take the command around, stripped from in single courts gave the part to the particular file. In my case, I don't know did that and kept in my workspace directory. So I'll goto the workspace location and this is a file. Once again, I will copy the part off the fight, then come back over here. Fish, that course complete. If I click on the run button here now it's gonna execute that man. And if there is no problem, it will give you some kind of update count, you can click the clear button and then look at the customer's. Here I have all the fields Clicking on the customers will bring me a select start from customers. Trick the run button, and I should be able to see all the customers that I have added through marco dot com. 6. Mapping Customer class to customers table: know that we have customers Data. Let's map our customer class that we wrote earlier to this particular table. Now for this, we need a few more dependencies. First, let me stop the cerebral if it is running, because every time I make some changes, the Sarwar keeps restarting and sometimes it becomes a night. So let's go back to start our spring there. Are you close this bombed or examine on? We want toe art this time. GP Springdale A GP provides a persistence mechanism to our application. So let's click on that. Get the new washing of the Bombing or XML computer to the Clipper. Let's go here replace our current formed or XML with the new one Andi as soon as they do that, newer dependencies have been added to my project, and the first thing that I want to do is to specify information about the DB on that I can do by going to the application that properties a couple off JP related information. Number one is what is the driver class that we want to use. So this is done by giving strict dot data source dot driver class name equals to our dark heads to dart Driver is the value for the same next spring. The data source dot you order You may get the value for this Europe from our H two point that's was distinct and then copy this one and then based over here. Similarly, I also want to give using me equals toe say password equals toe blank, so do not give any additional space. But along with this, you also have to give one more thing, which is spring dot jp a dot hibernated. Don't deal, Dash Otto. None Now, if you're familiar with hibernate, you know that there is something for hitch bm toe video. Otto were hibernate automatically creates their tables corresponding toe the customer job that I have written to say that we already have table and the leader corresponding toe the customer or job. They say that Haibin it should not manufacture the new table in the back in, but there ways your data along with the table will be deleted on newly constructed. So let's save this and go back to customer Gela. Now in the customer job, we have to say that this particular class corresponds toe the customer stable. There are two and traditions for doing this. Number one is Derek entity on This comes from Java Extort Persistence package. This is required because J. P. A. Which is Java persistence. AP. I will only consider classes that are marked with 100 entity now, since the table name is different than the class name, we may have toe specify that exclusively by typing are very table name. It wants to customers once again import the same thing from Java Extract Persistence package. Though this is not a case in to do, it is a good practice to follow. The lower case are uppercase for the table names. One other thing that you may want to mention here that the idea is the primary key among the others. So I can say here at the rate I d as the less If you want to say that this is automatically generated by back in with the average generated value, strategy equals toe identity. This is applicable for any column in the back and where auto underscore increment has been given. In case. If you have not given auto underscored increment but still want high benetto automatically increments this, then you can say generator equals toe increment. In this case, hibernate will automatically increments the value from the application rather than from the server. So, Cindy, in our case, we're given idea as art on the score increment. I can roll back toe strategy equals toe generation typed our identity. The other values for this could be something like sequence sequence is applicable for Oracle all you can also specify that it is table. So where a specific table would be created representing only primary key values. Or you can let it go automatically. By choosing Otto here. In my case, I'm gonna specify identity. If any of the columns are off different names than their respective field names, you may also want toe on a date that by at the red color. So in my case, all of them have the exact same name as the back end table columns. So I don't have to do that 7. Getting data from DB in our rest controller: now that we have done the mapping off customer class toe customers stable in the RPMs, let's know, create an interface that can give us all the values from the table. Now, this is where Springs Magic comes into picture. All we have to do is tow. Create an interface that extends something called correct repository and automatically Spring is going to create a class at Darren Time on the flight and gives an object off the same. Let's understand this by a practical example. Now in our controller, we needed toe return the list off all customers. So for that we have to get the data from the database. While we have done the mapping over here, the question is, how do I get the data from the database now? Traditionally, you might have used J BBC toe right select statement, get the later from the database and so on and so forth, or if you have used hibernate our GP eight, then you may want to use the session or into the manager to get the later on work with that , But in all cases, you have to read the court. But in this case, let's see, we don't have to write any court and still get the data from the database. Let's understand this by creating an interface called Customer Repository and let's change the package from controllers Toe Diego our repository, as you feel. And then importantly, we have toe extend this with credit repository. So let's say okay and then come on off this. Now you can see here that there is an error because there's an incomplete core. So we say that is the customer repository extending credit repository. Now crowd is an acronym for Create, Read, update delete. So this particular interface has already functions related toe inserting the data, retrieving the later updating, our believing the corresponding data. So we have to specify what kind of indeed the object that it works with. So he here is the type. So I'm gonna say, customer, and then I'd be Is the data type off the primary key value off this? If you go back to customer, you can see that the ivy annotation has been added toe integer. So this is the data type off our I d. So I go back to my repository and I say that the data type is integer and that's all I have to do. So if you click on this crowd repository and pressed function key four, you'll see that there are a bunch of functions over here. For example, Count gives me the number off customer objects that are available in the database billet deleting an object billet on off course. All of the records will be deleted. There is also find all exist by i d delete by i d. Save which is meant for inserting as well as updating depending on whether the Primerica is no are there is a value So all these functions are already available in our interferes. One last thing to do here is toe mark this with activate repository This way Spring knows that whenever we want an object off this class, it will create a class and then instruction and give that same thing to us. How can I use this one now? That is where we go back to our customer rest controller. And we say that Hey, we need an object off customer repository. The let's color does report will also make them as private. No, no, it is that this is a member variable of this class or bike before it is now. So we want to use this ripple on, then return an object. So we're gonna remove this C one and C two and obviously a raised art as list. I would simply have to say report art find all. Now you can see that the final is an irritable on. Beatable is a super type for list so we can't really do a return Now there are two ways to handle this scenario Number one, I can do it casting all I can change this from listed toe. Terrible. Now there is no problem. I can work with this, but there is only one issue here. Since this is a member variable, the default value for this is none. Obviously none dot Final is gonna be a null point of exception. This is where we have to tell spring that hey, when dysfunction is being called were actually needing an object of repository which is a customer repository and the customer repositories an interface. We want an object off the data type and I'll have to do is to tell spring that Hey, I need an object of customer repository and How do we do this? We simply say Activate art aware now when you say afterwards, Spring knows that this is a subtype off career depository on Spring knows every off creating a class because we have given what is the name of the class like customer. What is the primary key value on everything automatically is done at classes created on the fly on an object is also clear on the flight. That object is given to me here, so it is no longer now on it, since it is not know it's gonna go and get the data from the database. So let's go and organise imports by pressing control. Shift on Windows Command shift on Mac so everything is intact. Let's run our application and see if you get the desired output. We got one better here. That is, cannot lower driver class are their heads toward a driver. That's because we said in our application that properties that the driver class happens to be this one and it's not dead in any of the jobs. Now we could handle this in two ways. Number one. You can go back to a spring initial either and then specify that we want to look for hitch to database and then get the bomb dot XML, etcetera, etcetera. But ultimately you can add the jar directly from the hits to job that you download it earlier. So I goto Project properties and then select Joe ability. But libraries are external jobs, and then I go to my home directory where I have the hits, too. Been on day, we have hits to dash, wondered for that 1 $97 job open and then say Apply in close. Let's read on the application doctor once again and see if you got any more letters. Looks like everything is OK. Let's go back to our local host. Double seven double seven slash AP a slash customers Let's refresh and we should see 1000 records over here. 8. Adding pagination: now that we have a list off 1000 customers, quite often, we don't want all the customers in the database, because what if I have a 1,000,000 records off test Imus Now? You don't want to send all 1,000,000 records at one sharp, but human, 1% a chunk off there. For example, when a search anything in Google, Google may give you probably 20 or 50 reports at the first pitch. And then, if you want, you can go to the next page and so on and so forth somewhere I should be able to say that. Hey, give me the page number one something like this by typing question Mark underscore. Page equals to one. So this way I should get only the first page record where the first, which may consist off 10 reports, are 20 records, something that we can assume as default. So to do that, I can go back over here toe clips and then change our customer repository. Instead of extending credit repository, we can extend a subtype of card, repository, card, paging and starting repository, So let's go out that year. Beijing and starting repository save this. I have all the functions But in addition, we also have a couple of extra features. So if you go back over here on the refresh here, I still see my 1000 records. Of course, now I have a day off processing this page. Number one. Now the underscore page is a convention that you say that the page number is one. Similarly, if you want to say the size of the pages 20 for example, you may say 1% underscore. Limit equals to 20. So these are not any keywords. It's your choice. If you want to change this from Underscore page, toe paid size and be yell, etcetera. It's totally up to you. I'm going to stick with underscore Page underscored Limit for two parameters. Now, if I don't mention anyone off them, it should be assumed. For example, if I don't mention any perimeters at all, it should be assumed that I'm looking for page number one. So to do this, let's go back to our core in our rest Controller, we're going to see here something like this. So we're gonna add a couple of variables that they individual beige and then as a into your size, if page numb and paid says have been supplied. I can supply those two information over here. Now, if you look at the find all parameters, there is a sword object, I reckon, supply. There is an terrible ideas I can supply. And there is also Page Herbal I can supply. So Page Herbal is an interface that has a couple of options. So let's see how we can clear that. So I'm going to see your payable B equals two, which means information about the page ability such as page number and paid size, is gonna come from page request. Don't off. I can supply the page numb and obss ites. So I'm gonna say your page number. And then there's a big states on this page. Herbal needs to be imported from spring framework. So let's do that. And then I can say find all off key. So we do have now a page herbal option. But only one problem is that we did not tell spring where to look for values for this page name and paid size. Now, if you remember, we used our we start off using underscore page on underscore Limit. So to do that, we may have to say here something like I put it Request para meter on the perimeter Name happens to be underscore Page and then I can say if it is not supplied, default value is one. And that's what actually we have to say for patient. And then similarly, I can give here also an option saying that it is supposed to be underscore limit and the default value happens to be 10. So in Calif, you don't supply the limit will assume that it's gonna be default now, once month thing here Is that for us, the page number one means that we want to start from the record number one. But the page request trips. The dice page number do so because the index of the page start from zero. So we may want to specify here that page nom minus one because we want to say when you want page number one, we want to say you're zero. So as soon as we done with this, let's save it. Let's go back here and not specify any additional parameters and then refresh. I should see a smaller chunk here now, and you can see that it start from page number one. So hands I d number one to the last one, which would be the I. D Number 10 but we also get a lot of additional information. But I'm only interested in the area of detente safe. That is the case you might have to say. Here, Find out, Dart, get content. And that gives me a list off customers only. So when I go back here now, when I refresh there, you can see that we get a plain and simple a day off data from 1 to 10. If you want the second page, you can always a question more underscored page number equals two, and you can see we get it from 11 to 20 and so on and so forth. So if I said page number 33 we got 321 onwards up to 330. That's how we're getting the results. Of course, we can also check their head. Poor page. I want 20 reports, not 10. So I can say 1% underscore. Limit equals to 20 or 25. Whatever that you feel right now, you can see I got 20 records starting from 641 9. Postman for making requests: before we go any further, let's use a tool called Postman. So go to Google and then look for postman. For Mac. You've got on, Mac, are you can save indoor sickle on windows and then it says here, get postman dot com Download sick. You go to that you should be able to see based on your operating system. A download button is given. So since minus macro is, I'm able to see that it's Mac OS application Now what this does is it allows us to make a request based on the method, like get put post delete. For example, on the browser. When I typed here and press enter, it was always a get mattered and not post method. Also, at some point in time, if you want to send additional details like hitters, you will not be able to send from the browser. But from postman, you will be able to do this now if you want. You can also look for other rest clients, but I use postman, so let's go ahead and download and use that. So in my case, though I only have God Postman installed, let me open the same and then make a request for our local host 7777 in the a slash customers. So that's so that's had a new request that request IBIs. Get the Ural Happens to be http local host Colon Double seven, Double seven ascended. We're gonna get the hello from spring. I believe that's the response. Follow spring Boot. But if I say you're slash FBI slash customers, I said send. You can see that we got the customers from 1 to 10. So it also specifies that the status is 200. Total time taken is 56 milliseconds. What's the size of it and so on and so forth. Now, if you want to send some additional parameters, you can just put a question mark over here itself or reconsiders a section here. Corp Adams Key happens to be Let the underscored page on the value happens to be, Let's say, four. Andi, you can see that automatically updated Over here, I say send and I'm going to get from 31 to 40 which is the content off page number four 10. Get one customer: Now that we have got a to like postman, let's are more functionalities. For example, instead of getting all the customers are off a particular page, whatever I'm looking for a customer by a idee. So, for example, I can say customers slash 66 where I want to say that Gimme customer whose ideas? 66. Without mentioning the world, I'd be so we're not sending a quiet listing perimeter like question. Mark I d equals Store 66 but we're saying here that slash 66 So we want to assume that this 66 year is a variable or ivy. So let's go and add one additional function. This time I won't say public customer because I want to return a single customer. That's call it as get customer by highly. We want an integer Eylea on. Then I can simply say, return a little dot find by idee off. Heidi. This is that the return type is not a customer, but something got optional on. All I have to do is to say dot get and I get an actual customer object based on this idea. The question is, how do I map this? Because slash Abia slash customers cannot be mapped to two functions. We have to have some kind of difference. So we also say they get mapping. But this time it is for something with a perimeter. So the cuddly black It here is to say that this is a place holder on whatever is given over there is now recognised as I d. So it's gonna be like slash ap slash customers slash 66 that 66 is now identifiable by using the world idee. And now we want to say that whatever is there in the I d should be given to this now. Previously you so we said it's a request perimeter, but this time it's not a request perimeter. It's called us part variable. I have to simply on your attended part. Variable on. That should be good. Now, in case if the value that you're given your idea is different than the name of the variable , then it won't happen. So all you have to do in that case is to say that this is idee since in my case, the perimeter idea here is same as the variable name. This part is optional. So let me get rid of that, so I'm gonna save it. And now, if I go back to Postman and then send this request, I should be able to get a single customer data. So let me check it out by typing a different idea. Let's for example, 45. Send it on. We do get a different object. What if I type your 4500 where I don't have the corresponding data? Now what happens is that you simply get an exception. So it says your 500 internals there were better. And that's because there is a no al you present and this exception was actually created by a spring and hibernate combination. Now, if you don't want this exception on, if you don't want this to be reported as Internet, sir, where it out? Because it's just that we don't have a value for this, i d. It's still a proper response from the server, but I don't have a response content. If you want to do anything different, then what we can do here is to surround this with a try, catch block and then return a customer if it is phone. But return and novel you are a plank if there is no customer. So the first thing I'm gonna do is to change the data type here, toe something called response Entity off and put a question mark. Question mark is being very generate. We say that we want to send some response, but we don't know what is a data type because it can be a customer. It can be annulled value as well. So the number one thing that I'm gonna do is to say, get up this first and then I'll say Here, try Andi, catch an exception. So we're gonna say you're customer C one equals toe report out. Find my i d off. I d dot Get so we got the customer. But if there is an exception, I'm gonna go inside this and then I wanted it on something else. First, let me return a valued response from here, so we're gonna say response entity, Don't. Okay. And then we can say that Hey, c one is the body for this particular response because we have a valid values. And what is okay represents the state of scored 200. Whereas, if anything else is that I can probably send some other response. For example, I can say for not for saying that it is not phone instead of 500 so I can see your response . Entity. Andi, I can see your dart status off. I can say for not four and then I can say dot Body off none because I don't have any response to be sent on. Then I can say you're cynical. Off course I have to say return because I have to return some values of what? The places I have to say. Return Onda. Now you see that when there is data, I'm gonna say 200 then I'm sending that customer object. But if the data is not foreign, I'm sending a 404 but a body off now, so that's got tested out. So first, let's say 45. No problem in that. So we got the actual date of 45. But if I say you're 4567 which we don't have and not that previous or put is 200 status here. But when I send this now, we're gonna get for nor for and the body is now so there's no better as Internet star error 11. Adding new customer: another beer ableto get multiple records are a single record. Let's see how we can do adding in your card. So do that. Let's go Dollar application customer restaurant Roller. Let's add a new function here for Blake response in today Off caution mark on a new customer we received a customer object and then as a report out, save testimony. No, this actually acts the data that is coming from the perimeter into the database automatically because an assumption here is the customer ideas now and that customer object will be inserted into the database. The newly generated I d will be automatically set to this customer And then I can simply say here, But I've done the response entity Don't okay with the customer object like this. And we have to argue at the rate forced mapping to say that any request for slash ap slash customers with the http method called post should be handled by this particular function. Now the question is, how can spring give me a customer object woken as a user supplied the customer object. Now, if you come toe postman and choose a post my per for slash ap slash customers, you can see that there is an option here called Body and this is going on. Request Body or Taylor on You can supply a form data with name value pairs. Similarly, www for mural in Portage. Then there is a wrong, and then you can choose the type off text to be Jason or XML or plain text. So we are sending the actual customer later in Jason Format. But that's a request body, so we have to 10 spring that hate their data coming from the request body should be converted into a customer objective possible, and then that customer object is now taken on added to the database To do so. All I have to do is tow right at the rate requests. Body Onda. Now spring knows we're to look for a customer details when a poster questions may. Now let's give it a try by going back over here, we're sending a post request toe local news Double seven double seven slash CBs slash customers and that's what. Actually this is map to toe a B a customer's post mapping, but we have to send the body. I'm gonna send a body here that contains name off the customer. Let's at my name. That's a female. We don't have to be. No, not cool. That's a phone number. Gender City Bangle in Attica on a country is India. Now I'll have to do is to send this. Not that I'm not sending idee, but the response should be the same details with the newly generated I D. So let's click on the send button and see everything is okay. And as you can see that I. D. 1001 has been gendered it. And that response is 200. All I have to do waas toe read the data from the request body and saving to the reader base and then send the same customer back. So if I go back to the browser where my database I have access to here, so let's go connect, and then I can even check Select star from customers with Ivy close to 1001. Click the run button. I should see the same data that I have sent from Postman. It has been added successfully as well 12. Update customer data: as part of the crowd operations. Let's also provide update to our rest. Controller. Let's goto our controller here. I'm gonna add a function here that returns response entity off customer as a off stage customer. Now, as convention, we have to receive the ivy off the customer for whom we want to make changes on the customer data. It's them. It is possible that I may be different than the customers. I d. We have to discard the customers, I d and then use this for optation. So the first thing I will be doing here is testimony. Door said I'd be off i d. And then I'll say your reporter save customer. And if everything is okay, we just want toe written this one. What if something goes wrong? There'll be an exception, and that exception will be written as state a score 500 with a custom error message from Springboard. Now, as far as the mapping is concerned, we have to say here after it put mapping, we also have to say that this comes from a part variable this come from the request body. So we say here at the ridge path variable, whose name is gonna be idee and this is gonna be from Request Body. And since we said that it's supposed to come from part variable, we also have to say you're slash curly bracket. I d like this. So when you're making a request, you should say slash ap slash customers slash some i d. And that idea will be map to tow this. Plus the Baylor will be converted into a customer object and will be received over here. We're using the same function for save but distant. There is an I D. So because of which is going to do an update if possible. And if there is any other, this will be reporting status code 500 now. First, let's go and get one of the customer. Let's get the customer whose data is safe. 45. This is the data on. Let's copy this on. Then make a put request. Let's go to the body. You're wrong. And then I choose Jason and business. Now, even if you give some different idea here, it doesn't matter because this is an idea that would be considered for update. Of course, the rest of the later will be there Let's change this from the phone number ending with 7588 toe 0000 Some changes. Let's go and make the email to be example dot com. But it's also make this as that's a Dallas on Texas. So now I can click on the send, so see that the data has been updated, its going chicken or where the response and you can see that the response came back after it has been updated. So let's go also two very fine in the BB. Let's look for 45 then say Iran, and you'll see that the phone number and with 0000 city has been changed. State has been changed. The email is now activated. Example dot coms, everything has been updated. 13. Delete a customer data: Let's do delete operation first, you can see that I have the customer whose ideas? 45. So I click the run button. I see the data. Let's go back at a new function year, which is supposed to return their deleted customer data. Ansel's a response entity off customer type. It's called it as delete customer. We want to take the I d from the part really well. So I can actually copy this part and then based over here And then, of course, we have toe, sir, For this customer, if the customer is not form, we may want toe respond with 404 So let's do that. We had done that earlier here, so we can say that we want to get this customer so in a copy and paste the same thing. So we got the customer. But what if there isn't enter so the customer doesn't exist? So I put the try catch blocks under threat play catch Andi based or here. So if there is an exception, I'm gonna say you're that there is a for not for so I cannot delete this because the customer doesn't exist, so I can see that customer the nexus. But if the customer exists, I want to delete that first by doing a report dot did it? And then I supply either lead by idea or dilator entity. It's such like and say, See one and then I'll say your return response entity don't okay and then supply. See one Because see, one happens to be the data that we deleted. So as far as the mapping is concerned, it is like put mapping. But there is no request body, but we just started, say, after it village mapping and then with a slash idea, because we have to say whose record we want to do it. So that's another save. This spring Boot is restarting the application. Let's go back to the browser, Andi. Check once again. So I'm gonna say Select. Start from customers where Ideas 45. So we get the later let's go back to a postman. Now make the delete request. This time on Believed Request doesn't care about the body. There's no need off body, so I can remove that. So there's a deal. It 777 70 p a customer's 45. So I click on send of course it's gonna delete on. I would get the data back, but if I tried to send it again, you can see it's gonna be a four, not four, because there's no data. So I click on Send. Ah, now I see it is for not for because this record is not there in the database anymore. It's very find it by going to the database and click on the run button here and you see that no rose phone for I D 45. 14. Swagger for API Documentation: Now that we have done the rest ful Web series, it's important to add a p a documentation toe our surveys. Otherwise, when we shared this toe some customers who want to make use off other standpoint now it is very difficult for them. Toe read our understand or a PS and many implications we wouldn't be sharing the source score with them. So how do we do this? Not This is where you have a fantastic AP. I called Swagger. So if you go toe swaggered are are you? You can learn about how a P a development is simplified with the documentation off swagger . Now, I don't want to go too deep into how this works, but I want to show you how to add swagger, toe our application and get the A p a up and running. So the first thing we want to do is toe get this swagger. So if you go to spring initialize er and look for swagger here, you may not see that now, somehow, spring boot does not have the default. Dependence is included over here, so we may have to explicitly goto envy and deposited dot com and look for Strugar to press enter. You can see it come from I o Lord spring folks start spring folks Swagger too. So I'm gonna select that and the current washing happens to be 292 So I just had a copy of this. Go back to our bombed or XML at this as a dependency. So I can go to the end of the dependency list here and pasted escape this And now I have one dependency that is added. With this dependency, we will be able to scan and get information about all the AP is we have, If you want a graphical user interface that displays all the AP is provide some testing options, then you have the other one more dependency, which is from Let's goto swagger you I andi if you see that it's again from my own our spring folks spring folks swagger you, I The washing also is the same thing toe nine to a copy that once again go back to my palms or XML at as the last dependency Save it. It's gonna download the job fights for the same. There are two more steps in all in enabling swagger. Number one we goto application or Java and we say that we want enable swagger documentation . So I'm gonna go here and then type at the it enabled swagger and it says enable swaggered to and we have to add a bean definition here. So now what is this being definition? Now, let me say enable swagger toe. It's gonna look for an object that is going to scan your entire project for all the A P s and then build the documentation. Now this is called is a docket. And the way you do that is by typing public docket said documents supposed to come from spring Forbes documentations spring dark Web plug ins and then we can call any name we want . Let's call it best FBI and we have done some docket object. So with their return new dock it and then we supply a what Washington So you can see that it says, What's the documentation type? That's argument. So we think your documentation type dot swagger one off swagger, too. So we want to say swagger to Technically, we should be able to go Oregon. This object and everything should be okay, but this gives us conflagration as well. Now we have to mark this as a bean so that spring automatically instance shit. This object and then this doctor object can be used for building the A P A. Documentation Now to specify some configuration. Here we say your dark select. And then this is what to say what a B is to be selected with his select the AP eyes like dot AP eyes and then say What are the AP is that we want toe document on. We say Any request handlers are they? It's such as get mapping, post mapping. It's a trance. So we want that. So we say request handler. Selectors don't anything that any type of handlers we want. And then we also say any type off parts that we want to specify again, you can say what selector off any now. Once again, we are seeing that we want to just document everything but heroes. Well, instead of saying any, I can add some filter saying that a provide documentation for only get handlers, not for post Oregon, say slash ap a slash secured should not be imported. It starts that I can do that and then I can say build. So finally build. So a lot of documentation configuration. You can go over here. Then let's let's see. What is the error here? So we see some kind of a console log. Let's see. What is that? It is your class, not phoned docket. So this could be because when we actually added the mainland dependencies it waas still building. So we may have to stop this once. That's clear. The console on drum the application once again on see if everything is OK. All right. Looks like everything is fine. It's being scanning. You can see that. Scanning for FBI listing references. Tanker has been started. Application has been started in 10 seconds. So I'm gonna go back to the browser on first, look for something like local host 7777 slash and then I type your V two slash 88 docks. Now, this is where you will be able to see some Jason Documentation representing a lot of information. Let's copy this on open in. Postman on. Let's based it. Press enter. You can see that it's gonna give me a Jason representing lots off AP A yours. Now, this is not very useful for us. But this is gonna be used by swagger. You it. So how do I get into that? Very simple. You just have to stay here after that, you dot html But when I pressed enter, you can see we get a hollow spring boat on. This is because we have mapped the slash to this particular hello spring book. Now what I can do is I can just changes Toe slash. Hello? So that when slash Hello is asked. This is the onset that we're gonna get. If you're accessing the root off your service, then swagger you I should give the response in our buffet. Explicitly Access slash. Hello. We get this. Hello, string. Good. But if I think here Swagger, you write our HTML on now we should see that we got an a p A. Documentation over here. Now the a p a documentation includes application, which has a slash. Hello as one a. B. I. There's a basic error controller. No, we did not configure any other controllers, but if we do that, we will get. But there is a slash error with all get head post etcetera. So this is not configured by us, but you can see customer rest controller. That's the name we gave in our application. So efface expanded. You have slash AP. A. Customers get all customers is a function of the handler for the same. The post for a beer customers they get put on delayed for this particular you are. If you select a P A customer's, you can see that I'm able to supply some parameters on. So here it says there's an underscore limit Underscore paid as quiet string parameters that optional because they're the default value off 10 and one year, respectively. This could be the possible responses. If it is 200 there's gonna be okay. Example values and so on and so forth. Now what you can do is you can also test the same thing but thinking on trade out. So let's do that. When I do that, it says the two values defaults. Do you want to execute? Let's execute and see on it comes back and say, Hey, this is how you do where I see you are not call So I on a Mac in torture are Lennox like, and just copy this and based on I should be able to get it on a terminal. But if you want to access from the Broza, this is gonna be on. This is the response that we got our we were supposed to get when we make a request. And, of course, there's some options to download and so on and so forth. So this is how you use swagger toe create a P A documentation for all your micro services. 15. Building distributable bundle: now that we have a production really spring good micro service application in hand, let's see how we can build a distributable jar file on the run the jar file toe achieve the same thing. So the first thing that we have to do is to goto our eclipse in open the palm dot xml. The Bond, or XML, doesn't have dependency on hits to date of this because we added directly from project properties. And then we added, Why our job bill part like today? So we don't need this in this way because the May 1 told for installing all the dependencies looks only here, So we don't want that to be missed out. We can get rid of the itch to dependency from here so I can say removed. And then are the hits True dependency or here to get the hedge to dependency? Let's go back toe the spring initialize er, let's adhere age too. Click on this. When I say explore the project, we even see the palm, the XML. But this time I don't want to copy that entire Bonder XML, but I just want to get the kids to database dependency, so I'm gonna copy this. Go back toe clips in somewhere in between Angelo basis. The second thing is that because the X two databases is in the building part, Springboard automatically recognizes stage two on, then uses that as the driver. So we don't have explicitly mentioned in our application that property. So we don't need this. I can just make it other comment. Save it. And one last thing is toe build the jar file by right click on the project on the XML file . But on this May 1 in stock on this is gonna create a distributable jar file reading all the dependencies from our bombs or XML on. Finally, it said here, build success refresh, target folder and say explained, You see that there is a springboard demo 001 snatcher, Jack. Now I can open a command prompt or terminal on. Then around this job file, let's go to the United clearly knew that Let's get in tow their stop. We have this project for developing rest full services using spring boot and I have your A folder called Workspace. That's where my project is there, which is called springboard demo. Let's get in tow springboard demo. And then I have here a target folders. So if you take the pocket content I have here Springboard demo, snap short or char. So all I have to do is to type job Ominous John, target slash spring Good demo. So on. So jar. Fine. If I want to override any properties from the application, that property is fine. I can give you that before the target location as dashed capitally server port equals toe, for example, It zero or anything off your choice. Now, eijiro is the beef or pork for extra DP protocol, Which, which means you don't have to mention the port number while issuing the command. Now, if this is okay, we should be able to see our springboard console logs. And somewhere in between, it should also say, Http Sarwer started at port number 80 And once this is done, if I go back to the postman Andi, send this request again for 7777 We don't have this server running anymore, so it should not come back with an portraitist. Could not get any response. But either I can stay here 80 are I can completely remove the port number on this time. I should get the output properly 16. Thank you: Congratulations on completing this course. This goes end here, but learning continues. I hope you benefited from this course. Andi. Enjoy learning. As much as I enjoyed creating this course, I sincerely thank you for purchasing this course and wish you success in your future. If you have any doubts, are questions in any off my lectures and demonstrations. Don't hesitate to get back. Your feedback helps me employs ing my future courses once again. Thank you and wish you all the best.