Creating a Spring Boot REST API with iPad Pro and Raspberry Pi 4

The Controllers

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

Add the last layer before we can finally test our API. Spring Boot Controllers intercept the requests and process the data with the help of Service Components and Repositories.

Keywords

  • iPad Pro
  • Raspberry Pi
  • Code Server
  • Java
  • Spring Boot

About this video

Author(s)
Andrei Visan
First online
19 January 2022
DOI
https://doi.org/10.1007/978-1-4842-8060-7_10
Online ISBN
978-1-4842-8060-7
Publisher
Apress
Copyright information
© Andrei Visan 2022

Video Transcript

In this video, we will put everything we built in the previous ones together and create the Controller classes. Once this is done, we can finally test our API.

Let’s create a new package called Controller. And under here, we’re going to create our first controller, which will be the CategoryController. We annotate it with RestController. And also, I’m going to put a RequestMapping annotation here because what I want is that all my requests will start with api/v1/categories. And then for more specific URLs, I’m going to annotate the methods that I will build in the controller.

Now let’s inject the CategoryService. So I’m going to say private CategoryService. Think about, how do we inject the CategoryService based on the previous videos? You have a few seconds to think about until I finished writing this, then I will show you how to do it. So the answer to that is we use Autowired annotation. Good.

Now that we have this added, let’s start building the methods. The first method will be the saveCategory method, which will return a ResponseEntity of class Category. And the RequestBody, because this is a POST method, so the RequestBody for this one will be a category. I will annotate this method with a PostMapping, as it will be a POST request.

And I’m going to add here save. So in order to save a category, I’ll have to make a request to api/v1/categories/save with a RequestBody of a category. And here I’m going to return ResponseEntity.ok, meaning that everything went well. And then for the body, I’m going to return the CategoryService.save.

Now I want to retrieve all categories. So I’m going to create, once again, a ResponseEntity, which will return a list of categories. And I’m going to call this method findAll. Here it won’t be a PostMapping, but a GetMapping. And I’m not going to add anything for the GetMapping because just by going to api/v1/categories, I want to return all the categories.

So here what I will do, I’m going to say return ResponseEntity.ok again. And the body of my response will be a categoryService.findAll. And I’m going to create another method now to find a category by ID, which will take a path variable in this case. And I’m going to show you how this looks. And here I have to declare the value of the PathVariable, which should be id and Long id.

Now, here I’m going to have, again, a GetMapping. And here in the GetMapping, I’m going to declare slash and id. And this is the path variable. So whatever ID I get here in the GetMapping annotation, it will be sent to the findCategoryById through the id variable.

So now here I’m going to have a try, catch. And in the try, I’m going to return a ResponseEntity.ok with the body of categoryService. findCategoryById. In the catch, I’m going to return a ResponseEntity.notFound and build. Now, this will return an HTTP error not found.

Now another method I want to create is to be able to delete a category. So I’m going to say public void delete. It will be this time a DeleteMapping. And just like before, I’m going to have here the parameter id. And here all I’ll do, I’ll just say categoryService.delete based on the id.

The second controller that I want to create is the Payment System. Now the PaymentSystemController will be just the same as the CategoryController, so I’m going to skip over this. I’m just going to copy/paste all the code because it’s just exactly the same as the CategoryController.

So let’s create our last controller. That will be the ExpenseController. Once again, just like Payment System and CategoryController, this is just the same. So once again, I’m going to copy/paste the code.

Here I add three more methods, which are findExpenseByLabel, findExpenseByCategory, and findExpenseByPaymentSystem, which are just the same like the previous ones, just that we use the methods from the services that we created. So that’s it. Now we have all the controllers, let’s try to test what we have created.