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

Prerequisites

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

Install Code Server plugins and applications necessary to code our application.

Keywords

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

About this video

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

Video Transcript

In this video, we will install all the prerequisites so that we will be able to code our application. If you don’t have Code-Server running on your Raspberry Pi, and if you can’t access that via your iPad, then check my YouTube video on how to do that. I will link it at the end of this segment.

We will need to configure and install the following. We will install PostgreSQL on Raspberry Pi, and then we will install a client application on the iPad Pro so that we are able to access PostgreSQL server. We will install Spring Boot plugins for Code-Server so that we make Spring development easier. And we will install React.js plugins for Code-Server as well. And at the end, we will create a Spring Boot project.

Now let’s open the terminal. In my case, I use Blink. And let’s SSH into our Raspberry Pi. Once again, if you don’t have all figured or if you don’t know how to do that, check my YouTube video, which I will link at the end of this segment of. Good.

Now let’s run the first command, and that is to update our operating system. I have already done that. You can run it and then wait a bit until it finishes to run.

Once that is done, the next command we need to run is to actually install PostgreSQL. I already have installed it, but you can go ahead and run this and also wait a bit for the one to finish.

Next thing we need to switch our user to Postgres. Then we will have to create a user pi interactive. I already have it created, but I can go with you through the steps. You will enter the password here.

Then it asks you if you wanted to be a superuser. You should say Yes. In my case, it says it failed because I have already the role pi, but you can create it with the Postgres user. Go in and type “psql,” and then you will open the PostgreSQL terminal. Now here as you can see, Postgres stands for the database user that you are connected with.

In order to have our pi user working, we need to create the pi database. So next thing you need to do is to create the pi database, CREATE DATABASE pi. I already have that created. Now once you run this command your database will be created.

If you run backslash l, you will see all the databases you have there. I also created the database my_finances for our project. And you also see the pi database.

Now we can exit. And now we should be able to connect to our database with our pi user. So now if we type again “psql,” you will see that we can connect to the database. And this time we don’t have Postgres, we have pi.

Now we have to make a few more changes so that we are able to access our Postgres server from our iPad Postgres client. So first we need to edit the pg_hba.conf file. Let’s going inside.

And as we go down, we see here under Type Database User Address we have to add this line– host all all 0.0.0.0/0 md5. Once we have this added, we just need to modify one more file, and then we will be able to access our database. After we modify, type “wq” to write your file.

Now, the next file you need to modify is PostgreSQL.conf. Go on inside your file, and then you will see here a line saying listen_addresses. And go ahead and modify that from local hosts, which is default to 0.0.0.0, which allows us to access PostgreSQL from outside. Also if the line is commented, uncomment the line. Once again, type “wq,” and you should be able to write the file.

Now, in order to apply the changes that we just made, go ahead and do sudo systemctl restart postgresql. And in order to check if PostgreSQL has been restarted correctly, also check the status of PostgreSQL. And you should say active and running.

Next step let’s try to connect to our PostgreSQL server from the iPad. To do that, I have installed an application called TablePlus. Let’s open this application. And as you can see, I already have my database connection configured, but let’s configure a new one together.

Go here and add New, and a PostgreSQL Connection. In the connection name, you can give it whatever name you want. In the database host, this will be the IP of your machine. So you have to go 10.55.0.1, that’s in my case, and on the port, 5432, the default PostgreSQL port.

The database user, go ahead and put your pi user. And for the password, go ahead and put your pi password. The database name, I call it my_finances. If you gave it another name, type that name there, and then click Test.

If the fields are green, then you should be able to connect. So save your connection here. I’m not going to do that because I already have my connection. Now once you save it then you go inside, you should see functions and tables, of course. For now, we don’t have anything in here, so we’ll go ahead and close this.

And we can move forward with the plugins we need to install for Code-Server for Spring Boot. So first thing first, we need to type in “spring initializer” and install this one. As you can see, I already have it installed.

The next plugin we need to install is called Spring Boot Tools. Take the one from Pivotal, install it. As you can see, I already have it installed here.

And the last one is Spring Boot Dashboard. Go ahead and install this one as well. And you should be ready to start developing Spring Boot applications now.

But since we are here, let’s also move on and install some React plugins. For React plugins, it’s simple, just go and type “es7 react.” And then you’ll see this one here, ES7 React/Redux/GraphQL/React-Native snippets. I have this one installed as well. Go ahead and install this one.

And now we should be ready to go. We can start creating our project already. So we will start by creating the Spring Boot project. To start creating our Spring Boot project, first type Command+Shift+P, and here say “spring initializer.” And we go to create a Maven Project.

Now we will select the Spring Boot version, which is 2.5.2. The language will be Java. Here I’m just going to see– I will say io.programminglife for the group ID. The name of the project, let’s call it myfinances. I will select War as packaging. I think Java 11, it’s good enough.

And now we can start searching for dependencies. So first thing I’m going to say, Spring Web MVC. So I’m going to search for “web.” We can see it here, Spring Web. Next thing that I would like to add is the JPA, Spring Data JPA.

I would like to add Lombok. This is a utility so that we don’t need to write getters and setters, we can just use annotations. And I think that’s about it for now. Select the folder we want your project to be placed, and that’s it.

As you can see here, we have created our project. If you go under Java, you can see we have my application. Under the Resources, we have application properties. And here we will configure our database.

Now at the bottom of the Explorer side, you’ll see Spring Boot Dashboard. And here you can see MyFinances app is added to the dashboard. Now we can even start it from here. Probably we’ll get an error because we installed the JPA, and we don’t have configured the database.

But let’s start it anyway. And as you can see, it says “application failed to start” because we don’t have a data source URL. So that’s normal. There’s nothing bad there.

But, yeah, as you can see, we have Spring installed. We were able to run our app, even though it didn’t start successfully. So I guess we are ready to start coding.

Now as I promised at the beginning of our video, this is the URL where you can go and check in case you don’t have Code-Server on your Raspberry Pi and you cannot access it from your iPad. Go to this URL here, and you will be able to configure your Code-Server on your Raspberry Pi, and then you’ll be able to access it from your iPad so that we can work together on this.

I hope that this video is easy for you to follow. And now you have everything set up. So I’ll see you in the next one to start the fun part, the coding part.