Introduction à Spring Data REST

1. Vue d'ensemble

Cet article explique les bases de Spring Data REST et montre comment l'utiliser pour créer une API REST simple.

En général, Spring Data REST est construit sur le projet Spring Data et facilite la création de services Web REST basés sur l'hypermédia qui se connectent aux référentiels Spring Data - tous utilisant HAL comme type hypermédia pilote.

Cela supprime une grande partie du travail manuel généralement associé à de telles tâches et simplifie la mise en œuvre des fonctionnalités CRUD de base pour les applications Web.

2. Dépendances de Maven

Les dépendances Maven suivantes sont requises pour notre application simple:

 org.springframework.boot spring-boot-starter   org.springframework.boot

We decided to use Spring Boot for this example, but classic Spring will also work fine. We also chose to use the H2 embedded database in order to avoid any extra setup, but the example can be applied to any database.

3. Writing the Application

We will start by writing a domain object to represent a user of our website:

@Entity public class WebsiteUser { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; private String name; private String email; // standard getters and setters }

Every user has a name and an email, as well as an automatically-generated id. Now we can write a simple repository:

@RepositoryRestResource(collectionResourceRel = "users", path = "users") public interface UserRepository extends PagingAndSortingRepository { List findByName(@Param("name") String name); }

This is an interface that allows you to perform various operations with WebsiteUser objects. We also defined a custom query that will provide a list of users based on a given name.

The @RepositoryRestResource annotation is optional and is used to customize the REST endpoint. If we decided to omit it, Spring would automatically create an endpoint at “/websiteUsers” instead of “/users“.

Finally, we will write a standard Spring Boot main class to initialize the application:

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

That's it! We now have a fully-functional REST API. Let's take a look at it in action.

4. Accessing the REST API

If we run the application and go to //localhost:8080/ in a browser, we will receive the following JSON:

{ "_links" : { "users" : { "href" : "//localhost:8080/users{?page,size,sort}", "templated" : true }, "profile" : { "href" : "//localhost:8080/profile" } } }

As you can see, there is a “/users” endpoint available, and it already has the “?page“, “?size” and “?sort” options.

There is also a standard “/profile” endpoint, which provides application metadata. It is important to note that the response is structured in a way that follows the constraints of the REST architecture style. Specifically, it provides a uniform interface and self-descriptive messages. This means that each message contains enough information to describe how to process the message.

There are no users in our application yet, so going to //localhost:8080/users would just show an empty list of users. Let's use curl to add a user.

$ curl -i -X POST -H "Content-Type:application/json" -d '{ "name" : "Test", \ "email" : "[email protected]" }' //localhost:8080/users { "name" : "test", "email" : "[email protected]", "_links" : { "self" : { "href" : "//localhost:8080/users/1" }, "websiteUser" : { "href" : "//localhost:8080/users/1" } } }

Lets take a look at the response headers as well:

HTTP/1.1 201 Created Server: Apache-Coyote/1.1 Location: //localhost:8080/users/1 Content-Type: application/hal+json;charset=UTF-8 Transfer-Encoding: chunked

You will notice that the returned content type is “application/hal+json“. HAL is a simple format that gives a consistent and easy way to hyperlink between resources in your API. The header also automatically contains the Location header, which is the address we can use to access the newly created user.

We can now access this user at //localhost:8080/users/1

{ "name" : "test", "email" : "[email protected]", "_links" : { "self" : { "href" : "//localhost:8080/users/1" }, "websiteUser" : { "href" : "//localhost:8080/users/1" } } }

You can also use curl or any other REST client to issue PUT, PATCH, and DELETE requests. It also is important to note that Spring Data REST automatically follows the principles of HATEOAS. HATEOAS is one of the constraints of the REST architecture style, and it means that hypertext should be used to find your way through the API.

Finally, lets try to access the custom query that we wrote earlier and find all users with the name “test”. This is done by going to //localhost:8080/users/search/findByName?name=test

{ "_embedded" : { "users" : [ { "name" : "test", "email" : "[email protected]", "_links" : { "self" : { "href" : "//localhost:8080/users/1" }, "websiteUser" : { "href" : "//localhost:8080/users/1" } } } ] }, "_links" : { "self" : { "href" : "//localhost:8080/users/search/findByName?name=test" } } }

5. Conclusion

This tutorial demonstrated the basics of creating a simple REST API with Spring Data REST. The example used in this article can be found in the linked GitHub project.