In this video, look at a simple component to building a stand-alone application.
- [Narrator] Hi everyone. As a part of Sample REST Service, we'll implement a REST Service, which is a calculation resource and this calculation resource will have two endpoints: power and square root, so, the REST endpoints we will create a spring REST service and we'll also the manual testing of REST endpoints, our calculation API using postman client.
So, before we jump in, we need to walk trough some of the concepts; some of the annotations we are going to use to implement the REST resource, so I like to use the work resource; the first annotation; what we will learn today is REST controller, so REST controller is a class level annotation which allows you to declare other source and in that, you can have many matters which will be used for different functionality where you can map the UIs, so the two key ideas for a REST controller is the controller and response body, so this is the spring MVC annotations, so here, controller and response body are combined together and built into one REST controller, so REST controller is used to define the sources, so it's a class level annotation and it returns our domain object, so, in our example, in other sample, (mumbles) create the calculation API, so in calculation API, you can see like we will define two endpoints; one is for square root and one is for power, so it is a sample application and we are not focusing on all the thumb rules which should be there; our best practices to define the APIs, so our main focus is on learning the concept how we can implement the REST service, so first part is REST controller; let's move into our code, so you can see we are having this calculation controller class and since I want to map it and I want to create it as a REST resource, so what I'll do I'll simply write one thing over here.
REST controller, so now, it will have all the features which is required for implementing a REST resource, so now, moving to the next, after REST controller, the next important annotation is request mapping, so request mapping, so once you have the resource, you want to have the URI map to it, so you can see that the first part of this URI is calculation, so request mapping is annotation which we can use on both at class level and at matter level, so let's here, so what I'll do I'll write request mapping and what I'm planning to do I want this controller to map to calculation, so this calculation resource is now having the base part which is a calculation and then, we can have these matter level request mapping, so let's move it and you can see here, we are having request mapping power and we are having request mapping square root, so, request mapping is very important and it allows us to fond a URI.
Okay? So, now move to the next, so what we have done; we have done two important annotations; REST controller and request mapping, so these two we have implemented in the sense we have coded four calculation controllers, so now, we are having the mappings ready for these two APIs; now, the next important point for endpoint implementation is handling of the parameters, so, handling of the parameters are merely driven by two way; one is, you can pass the values as a query parameter or you can pass it using the part itself; the UI itself, so if you see like here, in power, I can pass it, let's say, like 144, so give me the value of; sorry, the square root, but I can have here also; I can pass two variables to the power, so let's say like power/two/four, so it would tell me like: Give me the value of which is equal to two is to the power four, but since it is something complex, so what we are doing for one method, we are going to use the request parameter and for one, we'll use the Path parameters, so for a square root, we'll use the Path parameter, so I can simply write square root/144, so it represents the value which is a square root of 144, so and similarly, for power method, we'll use the query parameters, so let's jump in of how we can use query parameters and Path parameters in Path core.
So, for query parameters, we'll use the request parameters, so that is request param annotation and for Path, we'll use the Path variable annotation, so, in request, it's very simple as you can see in the example for power method, we'll use the request parameters and we can pass the value of base and exponent, so let's do it, so here, we are having the power method and here, what we can do we can just simply tie request param annotation and we can give value to it, so the value is base; similarly, you can see here, we are having another request parameter which is having exponent as its value, so base and exponent we can pass as a part of request parameter by calling the endpoint calculation power and similarly, we can also define the Path variable, so for Path variable, what we are going to use we are going to use the Path variable annotation and in Path variable annotation, what we can do we can simply put the value in path itself; it will not pass query parameters, so that allows us have the dynamic values, so, Path variable you can use as a part of designing you APIs and you can make use of this dynamic feature, so whenever you feel like you have some value which can be used as a part of a UI itself, you should use the Path variable for implementation, so here, what we'll do, we'll move to square root, so in square root, if you see like it is having a request mapping in square root and we can define some value and this is the parameter, so we want to map this parameter to the Path variable, so what we'll do, we'll do the PathParam and the value we want to use which is defined here, so it's value only, so we'll write value, so, sorry, Path variable, yes, so now we have the Path variable and RequestParam as our way to pass the query parameters and similarly, Path variable for Path parameters, so now, our call is ready and what we can do, we can make our application executable and we can test it, so let's do it, so if you see like our application is REST application and since this is a spring application and we want to execute it standalone, so we can do simple one thing.
We can annotate it with spring boot application, so this is the fun thing we can do and similarly, what we'll do, we can save it; the changes, so like a spring boot application, it does three things for you, which is a part of spring boot application itself, so before we jump into the spring boot application and its internal things, let's revisit it, so what we have done; we have designed the API, calculation resource; the calculation resource is we are having square root and power and so, let's map it, so for resource, we use REST controller; for calculation, square root; these are the UIS, so for forming the UI, we use request mapping and similarly, when you have your endpoints, you pass the parameters and the values and those values and those values can be passed in the way; either query parameters or Path parameters, so for query parameters, we'll use the RequestParam and for Path parameters, we'll use the Path variable, so then, we develop our REST controller, the API for calculation and then, we have annotate our application with the spring boot application.
Now, the important point for our spring boot application it gives you three implicit annotations and what are these, let's learn it; so, you can see like here we are having three important annotations, so @EnableAutoConfiguration, @EnableWebMVC and @ComponentScan. So, AutoConfiguration allows you to put all your beans into the classpath and properties also.
Similarly, WebMVC, so since we are developing a REST service, it is a web application, we want to enable it; enable in a sense that the WebMVC and which provides all those internal changes required for application to be the web application, so, for example, like DispatcherServlet and @ComponentScan, so @ComponentScan; what it does basically wherever your class is there, so over here, if you see, RestSampleApp; this class is there and this base package and under this base package, we are having (mumbles) and also there are some packages of RestSample package, so we have defined our mean application class and REST sample package, so all these sub package will be scanned by this point for finding the beans configuration properties, etc.
So, this is where the spring boot application is handful, so we have our code ready and now, we can execute it, so let's execute it (mumbles) simple, since we are using Maven, so spring boot run, so now, you can see like the application has started on port 8080 and, okay, so before running the application, I have done one change like earlier it was calculations, so I removed those; now, let's test it, so here we'll execute it, so you can see like here what I'm doing.
I am passing the value 144 and the output is 12, so 12 is a square root of the value what we have passed 144. Simply, we can test it for, let's say like, we can put some different numbers; 7854.22 and let's see, so here the output is 88.6240 and long value, so now, we can simply see like we are having the dynamic values, so we are having a dynamic URI and similarly, we can see how we can use the power, so in power you can see like I'm passing two query parameters: base and exponent, so you can see like, these two we have declared here with request parameter, so these two values I'm passing the two and four and let's run, so I get 16; similarly, I can pass 12 and 14, so I'll get some exponent; value with exponent, so this is how it works, so let's put some small value.
Yeah. So, here, we are having the power value of where we have the base is 12 and exponent is five, so it is 248832, so this is the way we can develop REST endpoints and I feel it is helpful for you, so let me revisit what we have done. We have implemented the REST service which is having calculation API, square root and power using this spring framework, so in spring, we use REST controller; we use request mapping, we use query param and path variable.
These four annotations; important annotations for declaring the REST endpoints and similarly, to make executable application, we have use spring boot application annotation, so that's it for now. Thank you.
Note: To get the best results from this course, you should be familiar with the Spring Framework.
This course was created and produced by Packt Publishing. We are honored to host this training in our library.
- Writing sample tests
- Domain-driven design
- Strategic design
- Wrapping controls and pages
- Complex cases for pages and elements
- Testing microservices