liveBook · Manning

Spring Data REST

As explained in Spring in Action, Sixth Edition

Spring Data REST is a Spring Data project that automatically exposes repository operations through REST APIs.

Spring Data REST automatically exposes REST APIs for repositories created by Spring Data. Adding its starter to an application already using Spring Data enables repository operations without writing REST controllers. section 7.2

A Spring Data REST application provides:

  • Generated endpoints for repositories such as ingredients, tacos, orders, and users. section 7.2
  • Support for repositories created through Spring Data JPA, Spring Data Mongo, and related projects. section 7.2
  • Hypermedia links in responses, implementing HATEOAS navigation for clients that choose to use them. section 7.2
  • An alternative to manually written Spring MVC REST controllers when repository operations are sufficient. section 7.4
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>

Add the Spring Data REST starter to the build; autoconfiguration then exposes Spring Data repositories as REST resources. section 7.2

Inspecting an ingredient resource

After starting a repository-backed application, request /ingredients with curl to receive an embedded ingredient collection and _links entries for the collection and each resource. Request /ingredients/FLTO to inspect one ingredient directly. section 7.2

$ curl localhost:8080/ingredients
{
  "_embedded" : {
    "ingredients" : [ {
      "name" : "Flour Tortilla",
      "type" : "WRAP",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/ingredients/FLTO"
        },
        "ingredient" : {
          "href" : "http://localhost:8080/ingredients/FLTO"
        }
      }
    },
    ...
    ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/ingredients"
    },
    "profile" : {
      "href" : "http://localhost:8080/profile/ingredients"
    }
  }
}

The sample response shows an ingredient collection with embedded resources, self links, profile metadata, and resource links. section 7.2

How Spring in Action, Sixth Edition covers it

How deep it goes
Demonstrates dependency-only setup, generated endpoints, HATEOAS links, and curl requests.
Examples
Uses Taco Cloud resources, including ingredients, tacos, orders, users, and curl requests for ingredient endpoints.
What it assumes
Assumes Spring Data repositories already exist and relates them to manually written Spring MVC REST controllers.
What you can do after
Apply it via the Spring Data REST starter to expose repository-backed endpoints and inspect them with curl.
How it connects
Connects Spring Data repositories with REST APIs, Spring Data JPA, Spring Data Mongo, Spring MVC, and HATEOAS.

Also explained in

Compare how 2 books explain Spring Data REST →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.