‫Hi, welcome back. In this lecture, we are going to build create employee REST API and in next lecture,
‫we will write a unit test to test create employee REST API
‫All right.
‫So let's go and let's first build create employee REST API in this lecture.
‫Well, here you can see controller package right click on the control package.
‫new and then choose Java class.
‫Let's go and ahead and give class name as employee controller.
‫Great.
‫Now let's go ahead and let's annotate this employee controller with at rest controller annotation.
‫Well, as we are building, the rest API is using spring MVC,
‫we can use @RestController annotation to make this class as a spring MVC controller.
‫Now within this controller, we can define REST endpoints.
‫Well, let go ahead and let's use another annotation that is that request mapping annotation to provide a base
‫URL for all the REST APIs that we are going to define in this controller.
‫Our base URL should be /api/employees
‫All right.
‫Now, within employee controller, we are going to inject EmployeeService
‫Let's go ahead and let's create
‫Employee service instance variable
‫and then we will use constructor based dependency injection to inject employee service.
‫Well, let's go ahead and let's create a constructor
‫Right click Generate and then choose constructor and choose parameter
‫click Ok that's it now we created a constructor.
‫Well ideally, we need to use @Autowired annotation to inject this dependency, isn't it?
‫But spring 4.3 onwards
‫we don't have to add @Autowired annotation because whenever spring IOC finds a spring bean with a single
‫constructor spring IOC will automatically inject this dependency.
‫We don't have to use @Autowired annotation.
‫OK, so let's go ahead
‫and ignore this annotation because Spring IOC will automatically inject this dependency.
‫OK, great.
‫Now it's time to create REST API. Let's create a method public and lets you return type as Employee and
‫method name is create employee and let's pass method argument as employee object.
‫All right.
‫And let's have a return statement over here.
‫We gonna call employee service and its method to save employees to save the employee object
‫and just pass employee object.
‫All right now we have created a public method that is createEmployee().
‫Let's make this method as a rest API
‫by using spring annotations. Here you can see the method parameter
‫now let's go.
‫let's use @RequestBody
‫annotation to map request body to this object, in order to do that, we're going to use @RequestBody
‫annotation.
‫Well, basically @RequestBody annotation internally use as HTTP message converters to convert JSON
‫into employee object.
‫Now let's go ahead, and let's annotate this method with @PostMapping annotation so that this
‫REST API will able to handle HTTP post request.
‫Now this REST API returns by default the status code as 200 but in case of creating a new resource
‫our REST API should return CREATED as a status code to the client right. In order to define that status
‫let's go ahead and use @ResponseStatus annotation and just pass the status that we are going to return to
‫the client in our case status has CREATED.
‫All right.
‫Perfect.
‫Now this REST API is able to send a response with a status code as CREATED.
‫Now we have built create employee REST API successfully. In next lecture,
‫what we are going to do we are going to write a unit test to test this API.
‫By the way, you can also run the Spring boot Application and you can test this REST API by using the rest
‫client.
‫For example, here I'm going to run the Spring boot Application and this Spring boot application will
‫run, you know in a h2 in-memory database.
‫We haven't configured to MySQL database. In case of integration testing
‫we're going to use MySQL database.
‫Well, you can see here our spring boot application up and running on port 8080.
‫Now you can use a Postman client like this to test the rest API. Just type this URL
‫and select HTTP method a POST method and in body section just give a JSON with firstName, lastName
‫and email and choose content type as a JSON and click on Send Button. This will give
‫you the valid response.
‫It means that the REST API that we have built is working as expected.
‫And if you look at here status code 201 created.
‫OK, so you can test REST API using Postman client
‫but the objective of this course is to write a unit test to test the REST APIs. In the next
‫lecture,
‫what we are going to do
‫we are going to write the Unit test to test this REST API.
‫I will see you in the next lecture.