Integration and Building REST APIs
Complete source code is available here to download: Attach:advera.zip
- This session is based on the Spring platform
- Spring is not just a Java-based framework, but an ecosystem with family of Spring projects https://spring.io/projects/
- Spring Boot is one of the Spring projects used to create a micro Service.
Setting up the development environment
- These instructions are explained in this video
Install Java
- Verify the version of Java installed:
java --version
. If you don't get any result or any suggestions to install, then follow the below steps:
- Verify the version of Java installed:
- Update the package repository:
sudo apt update
- Install Java RE:
sudo apt install default-jre
- Install Default JDK (Java 11):
sudo apt install default-jdk
- If you have multiple version of Java installed on your system, you can decide which one is the default one.
- First, run this
sudo update-alternatives --config java
command to show all the installed version of Java on your computer - The choices are represented by numbers, select the number that's mapped to Java 11
- First, run this
- If you have multiple version of Java installed on your system, you can decide which one is the default one.
Install Spring Boot
- SDK Manager (sdkman):
curl -s "https://get.sdkman.io" | bash
- Spring Boot CLI:
sdk install springboot
- SDK Manager (sdkman):
Build Automation Tools:
- Gradle:
sudo snap install gradle --classic
- Upgrade Gradle:
- Add this PPA repository:
sudo add-apt-repository spa:cwchien/gradle
- Download package information from all configured sources:
sudo apt-get update
- Upgrade Gradle:
sudo apt upgrade gradle
- Add this PPA repository:
- Upgrade Gradle:
- Gradle:
Create Spring Boot Application
- Terminal:
- You can choose one of these option to create your Spring Boot Application.
- Gradle based project:
spring init --build=gradle --dependencies=web your_project_name
- ...with multiple dependencies:
spring init --build=gradle --dependencies=web,h2,data-jpa your_project_name
- Here is a full list of dependencies
- ...with multiple dependencies:
- Maven based project:
spring init --dependencies=web your_project_name
- Gradle based project:
- VS Code:
- IntelliJ IDEA (Ultimate):
- Browser (Online):
- You can also visit https://start.spring.io, an easy way to generate a template for Spring Boot project.
Run Spring Boot Applications
- Gradle:
- Build:
gradle build
- Run:
gradle bootRun
- Build:
- Maven:
mvn spring-boot:run
- Gradle:
Deploy Spring Boot Application (Gradle) to Heroku
- Create
system.properties
file in project root folder:- Add Java runtime version and save it:
java.runtime.version=11
- Add Java runtime version and save it:
- Create
Procfile
in project root folder:- Add the web process type and save it:
web: java -jar build/libs/Your-Project-Name-0.0.1-SNAPSHOT.jar
- Add the web process type and save it:
- Add default port number:
- Open
Your-Project-Name/src/main/resources/application.properties
and addserver.port=${PORT:5000}
, save, and start deploying.
- Open
- Create
PostgreSQL
- Refresh local package index:
sudo apt update
- Install:
sudo apt install postgresql postgresql-contrib
- Start:
sudo systemctl start postgresql.service
- Check Status:
sudo systemctl status postgresql.service
- Stop:
sudo systemctl stop postgresql.service
- Restart:
sudo systemctl restart postgresql.service
- Start:
- Login:
sudo -i -u postgres
ORsudo -u postgres psql
- Create a New User:
sudo -u postgres createuser --interactive
- Login with new role:
- Add user:
sudo adduser user_name
- Login:
sudo -u user_name psql
- Add user:
- Login with new role:
- Create a Database:
createdb database_name
ORsudo -u postgres createdb database_name
- List Databases:
\l
- Change Database:
\c database_name
- Show Database Tables:
\dt
- To reset the password if you have forgotten:
- Login without a password:
sudo -u user_name psql db_name
e.g.sudo -u postgres psql postgres
ALTER USER user_name WITH PASSWORD 'new_password';
- Refresh local package index:
- We recommend pgAdmin4 if you prefer to use a GUI tool for PostgreSQL administration.