Book Image

Spring Boot Cookbook

By : Alex Antonov
Book Image

Spring Boot Cookbook

By: Alex Antonov

Overview of this book

Table of Contents (15 chapters)
Spring Boot Cookbook
Credits
About the Author
Acknowledgment
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a Spring Data REST service


In the previous example, we fronted our BookRepository with a REST controller in order to expose the data behind it via a web RESTful API. While this is definitely a quick and easy way to make the data accessible, it does require us to manually create a controller and define the mappings for all the desired operations. To minimize the boilerplate code, Spring provides us with a more convenient way: spring-boot-starter-data-rest. This allows us to simply add an annotation to the repository interface and Spring will do the rest to to expose it to the web.

We will continue from the place where we had finished in the previous recipe, and so the entity models and the BookRepository should already exist.

How to do it…

  1. We will start by adding another dependency to our build.gradle file in order to add the spring-boot-starter-data-rest artefact:

    dependencies {
      ...
      compile("org.springframework.boot:spring-boot-starter-data-rest")
      ...
    }
  2. Now, let's create a new...