Using Spring Data REST

Spring Data REST is part of the Spring Data project. It offers an easy and fast way to implement RESTful Web Services with Spring. To start, using Spring Data REST you have to add the following dependency to the pom.xml file:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>

By default, Spring Data REST finds all public repositories from the application and creates automatically RESTful Web Services for your entities.

You can define the endpoint of service in your application.properties file:

spring.data.rest.basePath=/api

Now you can access the RESTful web service from the localhost:8080/api endpoint. By calling the root endpoint of the service it returns the resources that are available. Spring Data REST returns JSON data in the HAL (Hypertext Application Language) format. The HAL format provides a set of conventions for expressing hyperlinks in JSON and it makes your RESTful web service easier to use for frontend developers:

We can see that there are links to the car and owner entity services. The Spring Data Rest service pathname is derived from the entity name. The name will then be pluralized and uncapitalized. For example, the entity Car service pathname will be named cars. The profile link is generated by Spring Data Rest and it contains application-specific metadata.

Now, we start to examine different services more carefully. There are multiple tools available for testing and consuming RESTful Web Services. In this book, we are using Postman, but you can use tools that you are familiar with, such as cURL. Postman can be acquired as a desktop application or as a browser plugin. cURL is also available for Windows 10 by using Windows Ubuntu Bash.

If you make a request to the cars endpoint http://localhost:8080/api/cars using the GET method, you will get a list of all the cars, as shown in the following screenshot:

In the JSON response, you can see that there is an array of cars and each car contains car specific data. All the cars also have the "_links" attribute, which is a collection of links, and with these you can access the car itself or get the owner of the car. To access one specific car, the path will be http://localhost:8080/api/cars/{id}.

The request to http://localhost:8080/api/cars/3/owner returns the owner of the car. The response now contains owner data, a link to the owner, and links to other cars that the user owns:

Spring Data Rest service provides all CRUD operations. The following table shows which HTTP methods you can use for different CRUD operations:

HTTP Method CRUD
GET Read
POST Create
PUT/PATCH Update
DELETE Delete

Next, we will look at how to delete a car from the database by using our RESTful web service. In a delete operation, you have to use the DELETE method and the link to the car that will be deleted (http://localhost:8080/api/cars/{id}). The following screenshot shows how you can delete one car with the ID 4 by using cURL. After the delete request, you can see that there are now two cars left in the database:

When we want to add a new car to the database, we have to use the POST method, and the link is http://localhost:8080/api/cars. The header must contain the Content-Type field with the value Content-Type:application/json, and the new car object will be embedded in the request body:

The response will send a newly created car object back. Now, if you again make a GET request to the http://localhost:8080/api/cars path, you can see that the new car exists in the database:

To update entities, we have to use the PATCH method and the link to the car that we want to update  (http://localhost:8080/api/cars/{id}). The header must contain the Content-Type field with the value Content-Type:application/json and the car object, with edited data, will be given inside the request body. If you are using PATCH, you have to send only fields that are updates. If you are using PUT, you have to include all fields to request. Let's edit our car that we created in the previous example. We will change the color to white and fill in the register number that we left empty.

We will also link an owner to the car by using the owner field. The content of the owner field is the link to the owner (http://localhost:8080/api/owners/{id}). The following screenshot shows the PATCH request content:

You can see that the car is updated after you fetch all cars by using GET request:

In the previous chapter, we created queries to our repository. These queries can also be included in our service. To include queries, you have to add the @RepositoryRestResource annotation to the repository class. Query parameters are annotated with the @Param annotation. The following source code shows our CarRepository with these annotations:

package com.packt.cardatabase.domain;

import java.util.List;

import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResource
public interface CarRepository extends CrudRepository <Car, Long> {
// Fetch cars by brand
List<Car> findByBrand(@Param("brand") String brand);

// Fetch cars by color
List<Car> findByColor(@Param("color") String color);
}

Now, when you make a GET request to the http://localhost:8080/api/cars path, you can see that there is a new endpoint called /search. Calling the http://localhost:8080/api/cars/search path returns the following response:

From the response, you can see that both queries are now available in our service. The following URL demonstrates how to fetch cars by brand:

http://localhost:8080/api/cars/search/findByBrand?brand=Ford
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.251.57