Build a Rest Server in under 5 minutes with Spring Boot

iAwale
2 min readJun 3, 2020

Today, we will be creating a rest server using Spring Boot in under 5 minutes.

To get started head on to

start.spring.io

In our starter project we will need the following dependencies:

  1. Spring Web
  2. Spring Data JPA
  3. Rest Repositories
  4. H2 Database (In memory database)

It should look like:

Now, we import the project into our IDE, in my case I am using IntelliJ .

Your imported project will look something like this:

Lets create a Entity object which is basically a Table in your Database. In a file called main/java/com/awale/restserver/entity/Player.java

import javax.persistence.*;

@Entity
@Table(name="players")
public class Player {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String firstName;
private String lastName;
private String position;
private String team;

// Include Getters and setters
}

Now we need something that will handle the database connections, sql statements and everything related to persistence. This will be done by our PlayerRepository which will extend a JPARepository which does the actual magic.

In file main/repository/PlayerRepository.java

import org.springframework.data.jpa.repository.JpaRepository;@Repository
public interface PlayerRepository extends JpaRepository<Player, Long> {
}

This is all the code that is required to expose our Player entity to our rest server. We just have to add in a few configurations according to the database we are using. Since we are using an H2 database, we configure it as follows.

In main/resources/application.properties


spring.h2.console.enabled=true // console acts like a db manager
spring.h2.console.path
=/h2
spring.datasource.url
=jdbc:h2:mem:item

Since we are using a H2 database which is an in memory database we will add some db statements that insert data to test our rest server.

In main/resources/data.sql

INSERT Into Players (first_name,last_name,position,team) values ('Ram','Daniel', 'Striker', 'AFC');
INSERT Into Players (first_name,last_name,position,team) values ('Rob','Bott', 'Striker', 'AFC');
INSERT Into Players (first_name,last_name,position,team) values ('Trevor','Carry', 'Striker', 'AFC');
INSERT Into Players (first_name,last_name,position,team) values ('Trad','Daniel', 'Striker', 'AFC');

And this is it. You can run the application and test your rest server at localhost:8080/players

Thank you for reading. Please hit the like button if you found this useful.

The video guide is present below.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

iAwale
iAwale

Written by iAwale

Learn Productivity. Learn Growth. Learn Coding. Learn to Build Applications. If you like the content please follow Twitter Youtube

No responses yet

Write a response