Developing RESTful web service using Spring Boot, Spring Data JPA and Oracle Database
Ever thought of creating a webservice your app can consume in no time? Then look no further as Spring boot offers an easy way to create stand-alone, production-grade Spring based Applications in no time. The setup is pretty straight forward often needing less time to get it up and running.
Spring integrates a wide range of different modules in its umbrella, as spring-core, spring-data, spring-web-MVC, e.t.c.¹
The spring web-MVC leverages on the power of Dependency Injection to develop loosely coupled applications which when done properly could lead to effective and efficient applications
In this article, I’ll show you how to create a RESTful webservice with Spring Boot, Spring Data JPA and the data will be persisted in a local Oracle database. We’ll set up the project by using the following:
· Spring boot starter web
· Spring boot starter data JPA
· Oracle database 11g express
· Oracle JDBC driver ojdbc.jar
· Java 8
· Maven
· IntelliJ IDEA
1. Download Oracle Setup
First download the oracle 11g express database from oracle.com and install it on your local machine. You need to register if you don’t already have an oracle account before you can download the database. Extract the zip file and run the setup file. Follow the installation steps and do well to remember your password as you’ll need it to configure the data source. The default username is “system”.
2. Create a New Project in Intellij IDEA
Create a new project using spring initialzr from IntelliJ and add the following dependencies:
spring web and spring data jpa.
I assume you’ve installed maven, if not go here to set up maven.
3. Install Oracle Driver
The ojdbc.jar provides the necessary drivers and setup for oracle database.
To add the ojdbc driver to your local repository, download the ojdbc7.jar file from oracle.com.
Copy the jar file to any folder, preferably in your C:\ folder.
Run the following maven command:
For windows users, replace the forward slash with a backslash.
Add the following dependency to the pom.xml file:
Your project’s pom.xml should look like this:
4. Create The Project Structure
Since we are using the MVC architectural pattern which separates the application into model, view and controller, we need to create different packages for controllers, entities(or models), daos (Data Access object), and services. The project structure looks like this:
5. Configure Oracle Datasource on IntelliJ IDEA
On the database tool window, add a new Oracle datasource. Download the missing drivers if you’ve not done so.
Input the username(default is “system”) and password for your local oracle db and test connection.
It should be successful all things being equal. Click Ok!.
6. Add Application Properties
The application.properties file enables spring to know the database configuration and profile to use at runtime.
Navigate to your src/main/resources and add the following to the application.properties file.
7. Create the User Model
Next, we’ll create a User class annoted with the JPA @Entity annotation. This class creates a model which JPA uses to establish a relationship/mapping between the User entity and tables in the Oracle relational DB.
Navigate to the entity package and create a new class called User and add the following:
8. Add Data Access Object (DAO)
Next, we create a data access object which is an interface that extends JpaRepository interface. The DAO is used to perform CRUD database operations
Navigate to the dao package and create an interface called UserDao and add the following code:
9. Service Layer
The service class contains all methods that handle the business logic of the application. Navigate to the service package and create a new java class called UserService . For brevity sake, I’ll only create a service to add a new user and retrieve all users.
10. Controller
Next, we create the User Controller which holds all the REST endpoints. Navigate to the controller package and create a new class called UserController and add the following:
11. Run Application
Now the application is ready, open terminal on IntelliJ and run the following maven command:
This creates the User table in the database using the JPA annotations and the ojdbc configurations in the application.properties file. On the terminal logs, you should also see sql syntax on how the tables were created.
12. Testing on Postman
We’re going to test the REST endpoints from the UserController using Postman.
a. Add a new User
On Postman, you can perform a POST request to create a new user on the
/user/adduser endpoint
Here we POST a User object that has a name and salary. Here’s a screenshot of the POST request and the corresponding Json result:
b. Retrieve all users
Also, you can get all users on persisted on the database by performing a get request on the /user/all endpoint. The sample GET request is shown below:
Voila! So that’s basically how you can set up a webservice using spring boot. Thanks for reading.
Don’t forget to drop your questions, contributions, and thoughts in the comment section below.
You can clone the project on github
13. Useful Resources
[1] Difference between Spring MVC and Spring Boot
NOTE: You can contribute your ideas to this project for Spring boot beginners.
Ideas to consider:
1. spring boot with mysql or any other database
2. spring boot with kotlin
3. spring security
4. JUnit Test
5. Integration Test
6. Your own idea would be great…just do it!