Learn how Spring Data REST maps HTTP commands to Spring Data JPA CrudRepository interfaces methods.
- [Instructor] So let's get down to coding these APIs. First, let's code the API to get all the Tour Packages. We neeed an endpoint called tourPackages such that HTTP GET /tourPackages fetches all the Tour Packages in JSON format. HTTP GET /tourPackages/<code> fetches one tour package with the provided code. Ready, set, go! Done! I'm not kidding you. Because we included Spring Data REST as one of our project dependencies, there is no coding necessary to implement these APIs.
At application start up, Spring Data REST scans the classes and finds all the Spring Data repositories, creates an endpoint that matches the entity name, appends an s and exposes the operations as APIs. So here are the mappings for CREATE, the mapping for READ, the mapping for UPDATE and DELETE. So let's try it out. Our Explorer California application is running.
I'm going to use Postman to exercise the APIs. So HTTP GET on localhost:8080. It returns the list of all Tour Packages and it follows the house specification. We see that name is a string attribute. But instead of a code attribute, there is a links.self.href attribute. That is a hypermedia link to the individual Tour Package.
So let's click tourPackages/BC and invoke the GET method on that individual Tour Package. We get back Backpack Cal. Because the data is hypermedia driven, the ID of the resource is the self.href link. The client does not have to manually append the ID to the resource. It just gets the value of the self.href to obtain the entry in the list.
We are getting a taste of the beauty of hypermedia here. Most front-end developers prefer consuming hypermedia driven APIs because it makes their code smaller, cleaner and easier to maintain. Let's go through the CREATE, READ, UPDATE, and DELETE methods for the Tour Package API. I'm going to create a brand new Tour Package called Zany Cali. So I'm going to use POST, under raw data.
It's JSON format. So here's my JSON package. Setting the code to ZC and the name to Zany Cali. Then we send that request. We get back a 201 Created. This one instantiate a new Tour Package object and object the Tour Package that save method to persist it in the database.
The JSON data elements map directly to the Tour Package attributes. Since we are creating a new object, we must provide the ID because it is not auto generated. Let's try to create a Tour Package without a code. I'm going to remove the code and just say we want to create a Tour Package called Crazy Cali and we accidentally forget to put the code. We get back a 500 Internal Server HTTP status error.
Now let's look up the Zany Cali Tour Package. We get it back with the 200 code. Now let's rename the Tour Package to California Zoos using HTTP PUT. So the ID of ZC is on the URL and the body has the name, change for the click.
And that works. Finally let's delete the Tour Package. HTTP DELETE method on ZC. This is not necessary so we'll delete that body. Send that request. This invokes the Tour Package repository that delete method and return to 204 No Content response which is good. Now let's verify that by trying to look it up by using GET.
This returns a 404 Not Found because the object no longer exist in the database. Any errors that occur at the repository level are passed back in the HTTP status. For example, I cannot delete a Tour Package that's already associated with a tour because it will cause a referential integrity error. So let's try that out. Let's try deleting Backpack Cali. This returns a Referential integrity constraint violation message.
So all this time, we were creating repositories. We were also creating hypermedia driven APIs just because we included the Spring Boot Starter Data REST dependency in our pam.xml.
- Setting up the project
- Building, deploying, and launch the microservice
- Declaring Spring Data JPA repository interfaces
- Invoking repositories
- Using Spring Data query methods
- Exposing RESTful APIs with Spring Data REST
- Using the /search resource to invoke query methods
- Paging and sorting
- Declaring a new REST controller
- Creating HTTP methods for creating, reading, updating and deleting persistent data.