Tuesday, November 24, 2015

Exposing Spring Data Repositories over REST with Spring Boot and Spring Data REST

Spring Boot let us quickly create projects. Spring Boot favors convention over configuration. Most of the time we start with writing domain Entities. To quickly look around Entities or to populate some data we can use Spring Data REST. With Spring Data REST we can easily expose JPA based repositories as RESTful endpoints.

Most of the time you'll need to write your own endpoints for adding validations etc. But for testing the entities or when we need to try something it is good tool.

First we need to create Spring Boot project and add spring-data-rest as maven dependency to pom.xml file

  org.springframework.boot
  spring-boot-starter-parent
  1.3.0.RELEASE
 
 
  
   org.springframework.boot
   spring-boot-starter-data-rest
  
  
   mysql
   mysql-connector-java
   runtime
  
 

Let's create basic Entity
import javax.persistence.*;

@Entity
public class Customer {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    private Long id;

    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Spring Data's JpaRepository interface adds CRUD operations without adding any code. Spring Data REST will use this repository to generate REST endpoints.

@Repository
public interface CustomerRepository extends JpaRepository<Customer, Long> {
}

Then all we need to do is add @EnableJpaRepositories annotation
@EnableJpaRepositories
@SpringBootApplication
public class SpringDataRestDemo {

    public static void main(String[] args) {
        SpringApplication.run(SpringDataRestDemo.class, args);
    }

}

When you start the application at the root of your url you'll see HATEOS supported endpoints response like below. You can make GET/POST/DELETE/PATCH/PUT requests to this endpoints.
{
  "_links": {
    "customers": {
      "href": "http://localhost:8080/customers{?page,size,sort}",
      "templated": true
    },
    "profile": {
      "href": "http://localhost:8080/profile"
    }
  }
}

Let's check the customers
{
  "_embedded": {
    "customers": [
      {
        "name": "ugur",
        "_links": {
          "self": {
            "href": "http://localhost:8080/customers/1"
          },
          "customer": {
            "href": "http://localhost:8080/customers/1"
          }
        }
      },
      {
        "name": "Luke",
        "_links": {
          "self": {
            "href": "http://localhost:8080/customers/2"
          },
          "customer": {
            "href": "http://localhost:8080/customers/2"
          }
        }
      }
    ]
  },
  "_links": {
    "self": {
      "href": "http://localhost:8080/customers"
    },
    "profile": {
      "href": "http://localhost:8080/profile/customers"
    }
  },
  "page": {
    "size": 20,
    "totalElements": 2,
    "totalPages": 1,
    "number": 0
  }
}

{
  "name": "Luke",
  "_links": {
    "self": {
      "href": "http://localhost:8080/customers/2"
    },
    "customer": {
      "href": "http://localhost:8080/customers/2"
    }
  }
}