Let me start by saying that I really enjoyed the series of articles by Elleanor Kerry called “REST Overview.” Here are links to these articles:
- Overview of REST. Part 1: What is REST
- Overview of REST. Part 2: Communication between client and server
- Overview of REST. Part 3: Creating a RESTful Service in Spring Boot
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
To create a REST controller:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
For Tomcat server:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
For PostgreSQL:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.10</version>
</dependency>
Now that we've sorted out pom.xml, let's go to the resources folder and fill out the application.properties file as follows:
spring.datasource.url=jdbc:postgresql://localhost:5432/customers
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database=postgresql
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL10Dialect
Here we entered the URL of our database, provided the login and password for it, registered the driver for PostgreSQL, indicated that we would use the PostgreSQL data type, and specified the dialect for Hibernate. Next, let's create a new directory called database in the same resources folder . In this directory we will create 2 files: initDB.sql and populateDB.sql . The first will be responsible for creating tables, the second will be responsible for filling them out initially. Let's open initDB.sql and see a green bar at the top that says SQL dialect is not configured . This means that we have not chosen a SQL dialect for our project (and there are several of them). Click on the right side of the same strip on the inscription Change dialect to… . In the pop-up window, click Project SQL Dialect , and since our database is PostgreSQL , we select the dialect of the same name. Click OK Let's move on to filling out our .sql files . Let's first fill in the initDB.sql file :
CREATE TABLE IF NOT EXISTS clients
(
id BIGSERIAL PRIMARY KEY ,
name VARCHAR(200) NOT NULL ,
email VARCHAR(254) NOT NULL ,
phone VARCHAR(20) NOT NULL
);
If, after filling out the file, some words other than clients are written in white font, then right-click inside the text and select Change Dialect --> PostgreSQL again . As you probably already understood, this is the same data that we filled in when creating the test table manually. Here they are formatted in the PostgreSQL dialect of the SQL language. Now let's fill the populateDB.sql file :
INSERT INTO clients VALUES
(1, 'Vassily Petrov', 'vpetrov@jr.com', '+7 (191) 322-22-33)'),
(2, 'Pjotr Vasechkin', 'pvasechkin@jr.com', '+7 (191) 223-33-22)');
If the name of your clients table is written in red letters, then it’s okay. The point is that we haven't created this table yet, and IDEA doesn't recognize it yet. In order to create and populate the table, we need to go back to the application.properties file and add the following three lines there:
spring.datasource.initialization-mode=ALWAYS
spring.datasource.schema=classpath*:database/initDB.sql
spring.datasource.data=classpath*:database/populateDB.sql
In these lines we say that we want to initialize the database programmatically and indicate which files need to be used for this. Next, go to the main method of our application and launch it . After that, go to pgAdmin --> Servers --> PostgreSQL 12 --> Databases --> customers --> Schemas --> public , right-click on " Tables ", " Update ". If everything went well, then we see the clients table we created . After that, go back to the application.properties file and comment out the line.
spring.datasource.data=classpath*:database/populateDB.sql
as below:
spring.datasource.url=jdbc:postgresql://localhost:5432/customers
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database=postgresql
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL10Dialect
spring.datasource.initialization-mode=ALWAYS
spring.datasource.schema=classpath*:database/initDB.sql
#spring.datasource.data=classpath*:database/populateDB.sql
If we do not do this, then the next time we run the program we will receive the following error: org.postgresql.util.PSQLException: ERROR: A duplicate key value violates the unique constraint "clients_pkey" . This happens because we have already filled in the fields with id 1 and 2 in the table (even during the first launch). In the table we created, the id field is specified as bigserial , which corresponds to the Long type in Java. However, in our program the type for this field is Integer . I decided to show how to use Long (BIGSERIAL) because it has a larger range than Integer. The fact is that in tables, a field designated as Primary Key can be used not only to store user ids , but also to store indexes of a wide variety of data, and the number of such records can exceed the maximum Integer value. For example, if our program takes some measurements every second and writes the data to a table. In order to rewrite our classes to use the Long data type, we need to change the type from Integer to Long in all classes and methods that use the id field . We will not do this, because the program was originally written by the author for the id Integer type, which means this makes some sense. To continue, let's once again remove the clients table we created from our database, but now we'll try to do it programmatically rather than manually. To do this, comment out our code in the initDB.sql file and add one line:
-- CREATE TABLE IF NOT EXISTS clients
-- (
-- id BIGSERIAL PRIMARY KEY ,
-- name VARCHAR(200) NOT NULL ,
-- email VARCHAR(254) NOT NULL ,
-- phone VARCHAR(20) NOT NULL
-- );
DROP TABLE IF EXISTS clients
Let's launch the program, go to pgAdmin , right-click on “ Tables ” (in our customers database ) - -> “ Update ”, and we will see that our table has disappeared. NB! Be careful when using this command, otherwise you risk losing all the data you had in your table! Let's go back to the initDB.sql file and rewrite it as follows:
CREATE TABLE IF NOT EXISTS clients
(
id SERIAL PRIMARY KEY ,
name VARCHAR(200) NOT NULL ,
email VARCHAR(254) NOT NULL ,
phone VARCHAR(50) NOT NULL
);
Here we have changed the id type to SERIAL , which matches the Integer type we are using for the id field in our program. In addition, the maximum length of the phone field has been increased so that we can freely use spaces and special characters (parentheses, dashes, etc.) in its writing. The maximum number of digits in a phone number is currently 18 digits (if my memory serves me correctly). I set the size to 50 characters to be sure. Let's go to the application.properties file and uncomment the line:
spring.datasource.data=classpath*:database/populateDB.sql
Let's run our program, go to pgAdmin, check that our table has been created, and comment out this line back.
#spring.datasource.data=classpath*:database/populateDB.sql
This is where I’ll probably finish the first part of the article. I hope you like it, write comments (even if you didn’t like it). In part two, we'll rewrite our classes so they can work with real databases. Continuation: Adding a PostgreSQL database to a RESTful service on Spring Boot. Part 2 UPD Thanks to the moderators for correcting my pictures and text formatting!
GO TO FULL VERSION