Writing a RESTful API with Spring.

I have recently been reading and learning more about Spring and finally decided to work on something myself to learn better. So I decided to write a simple RESTful API. I will document my progress in this blog post as I go.

Creating a Spring Boot project:

I start by creating a Spring Boot project using Spring Initialzr. Dependencies I will use in this project are:

  1. Spring Web Starter
  2. Spring Data JPA
  3. PostgreSQL Driver

Download the zip and open it up in your preferred IDE.

Setting up the database:

I already have PostgreSQL installed so I just create a database and add the following lines to the application.properties file.

spring.datasource.url=jdbc:postgresql://localhost:5432/books
spring.datasource.username=postgresql
spring.datasource.password=postgresql
spring.jpa.hibernate.ddl-auto=create

Here spring.jpa.hibernate.ddl-auto=create means each time the schema is recreated and previous data is destroyed.

Creating Entities:

I will create two entities called User and Book.

User.java:

import javax.persistence.*;

@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private long id;
    @Column(nullable = false)
    private String username;
    @Column(nullable = false)
    private String password;

    public User() {}
    //Getters and setters omitted.
}

Book.java

import javax.persistence.*;

@Entity
@Table(name = "books")
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private long id;
    @Column(nullable = false)
    private String title;
    @Column(nullable = false)
    private String author;
    @Column(nullable = false)
    private int pages;

    public Book() {}
    //Getters and setters omitted.
}

@Entity marks the class as an database entity. @Id declares the identifier for the entitiy. The other annotations such as @Table and @Column are optional. I used @Table(name = "users") to override the default table name user which is a keyword in PostgreSQL. Unless changed with @Column(name = ""), @Column uses the field name as the database table column name too.

Creating Repositories:

IUserRepository.java:

import com.erenkibar.booktracker.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;

public interface IUserRepository extends JpaRepository<User, Long> {
    public List<User> findByUsername(String username);

}

IBookRepository.java

import com.erenkibar.booktracker.model.Book;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;

public interface IBookRepository extends JpaRepository<Book, Long> {
    public List<Book> findByTitle(String title);
}

The JPA supports deriving a query from the method name. The method name has 2 parts seperated by the By keyword. So the method findByUsername() gets translated into something like this:

select u from users where u.username = ?;

Since the JpaRepository interface extends PagingAndSortingRepository which, in turn, extends CrudRepository, we by default get methods for sorting, paging and CRUD operations. Such as findAll(),findById(),findAllById(),delete(),deleteById() etc.