Includes functional sample projects that demonstrate the described concepts in action and help you start experimenting right away
Provides step-by-step instructions and a lot of code examples that are easy to follow and help you to get started from page one
Full review
My initial impression of this book is that it's too short and lacks full coverage because it only focuses on Spring Data JPA and Spring Data Redis. We all know how big the Spring Data umbrella projects are. But it turns out this is the strength of the book. By focusing on a subset of Spring Data umbrella projects, it's able to focus better on what matters most.
As I read the book, I slowly realized that this book is a gem. If you need a solid understanding of Spring Data JPA, read this book. It tells you step-by-step all the possible query technologies, usage patterns, and their pros and cons. The book gradually prepares the reader to the value of Spring Data.
The Spring Data JPA coverage is quite extensive. It teaches you how to download and install the necessary libraries. Configuration is based on programmatic configuration instead of the usual XML configuration files. I think this is good but also bad. It would be great if the book offers sample configuration both in XML and Java-based config. Since most users are familiar with XML configuration, translating from Java-based config would require extra effort to comprehend for most Spring users. Anyway, that's a minor weakness that we can live-up with.
The book is successful in demonstrating how to provide CRUD support through Spring Data JPA and how to implement your own custom repository. There are various way to perform queries in Spring Data JPA, and I think the book has managed to cover all of them, including QueryDSL.
The book's coverage on Spring Data Redis is extensive. It covers installation and configuration, connector types, Redis data structures, and of course, Spring Data support for Redis. The book teaches how to save relational data and perform CRUD operations in a NoSQL manner. It also covers messaging and caching support with Redis. Overall it's a pleasant read. It's interesting how the book has smoothly transitioned from Spring Data JPA to Redis.
Overall, Spring Data from Packt Publishing is a solid book that I recommend to everyone to read.
In this tutorial, we will update an existing Spring MVC app to Spring MVC 3.2 and add RESTful endpoints using Spring Data Rest. The goal is to demonstrate how we can implement HATEOAS methodology using Spring.
Github
To access the source code, please visit the project's Github repository (click here)
Update the pom.xml
Here are the changes that we need to do:
Update the Spring core version
Update the Spring Data JPA version
Add Spring Data Rest dependency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Note: I also added the profiles section at the end of the pom.xml, so that we can expose the repositories in a clean manner.
Update the web.xml
Here are the changes that we need to do:
Update the web-app version to 2.5 (optional)
Update the display-name (optional)
Add Spring Data Rest servlet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Open the spring.properties under WEB-INF folder, and update it as follows:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In the original application, the declared database name is spring_jqgrid_tutorial, let's update it to spring_data_rest_tutorial (though this is really not needed).
Create a spring-data-rest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
There's not much update here. We just need to import the spring-data-rest.xml as follows:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The only update here is the addition of the annotation @Param to the UserRepository. This is required so that we can expose the parameters in the search queries.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
We need to create a new repository for the Role domain so that we can expose it as RESTful endpoint:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In the previous section, we have updated our app's configuration, so that our repositories are exposed as RESTful endpoints. We've also applied the HATEOAS methodology which "serves to decouple client and server in a way that allows the server to evolve functionality independently" (Wikipedia). In this section, we will build and run the application using Maven, demonstrate how to import the project in Eclipse, and run a series of manual tests to examine the RESTful endpoints.
HATEOAS, an abbreviation for Hypermedia as the Engine of Application State, is a constraint of the REST application architecture that distinguishes it from most other network application architectures. The principle is that a client interacts with a network application entirely through hypermedia provided dynamically by application servers. A REST client needs no prior knowledge about how to interact with any particular application or server beyond a generic understanding of hypermedia. Contrast this with e.g. a service-oriented architecture (SOA), where clients and servers interact through a fixed interface shared through documentation or an interface description language (IDL).
The HATEOAS constraint serves to decouple client and server in a way that allows the server to evolve functionality independently.
Source: http://en.wikipedia.org/wiki/HATEOAS
Running the Application
Access the source code
To download the source code, please visit the project's Github repository (click here)
Preparing the data source
Run MySQL (install one if you don't have one yet)
Create a new database:
spring_data_rest_tutorial
Import the following file which is included in the source code under the src/main/resources folder:
spring_data_rest_tutorial.sql
Building with Maven
Ensure Maven is installed
Open a command window (Windows) or a terminal (Linux/Mac)
Run the following command:
mvn tomcat:run
You should see the following output:
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'tomcat'.
[INFO]
[INFO] --------------------------------------------------------------
[INFO] Building spring-data-rest-tutorial Maven Webapp 0.0.1-SNAPSHOT
[INFO] --------------------------------------------------------------
[INFO]
[INFO] Preparing tomcat:run
[INFO] [apt:process {execution: default}]
[INFO] [resources:resources {execution: default-resources}]
[INFO] [tomcat:run {execution: default-cli}]
[INFO] Running war on http://localhost:8080/spring-data-rest-tutorial
Nov 20, 2012 8:01:45 PM org.apache.catalina.startup.Embedded start
INFO: Starting tomcat server
Nov 20, 2012 8:01:45 PM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.29
Nov 20, 2012 8:01:46 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Nov 20, 2012 8:02:01 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'rest-exporter'
Nov 20, 2012 8:02:03 PM org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
Nov 20, 2012 8:02:03 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Note:If the project will not build due to missing repositories, please enable the repositories section in the pom.xml!
Access the grid page
This displays a grid that allows us to experiment with the data visually.
Follow the steps with Building with Maven
Open a browser
Enter the following URL (8080 is the default port for Tomcat):
http://localhost:8080/spring-data-rest-tutorial/
Access the RESTful entry endpoint
We've declared entry endpoint in spring-data-rest.xml and web.xml files.
Examine the grid and we shall see the new record as well:
Note: You can do more with Spring Data Rest. I suggest reading the docs further for more info.
Import the project in Eclipse
Ensure Maven is installed
Open a command window (Windows) or a terminal (Linux/Mac)
Run the following command:
mvn eclipse:eclipse -Dwtpversion=2.0
You should see the following output:
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------------------------------
[INFO] Building spring-data-rest-tutorial Maven Webapp 0.0.1-SNAPSHOT
[INFO] ---------------------------------------------------
[INFO]
[INFO] Adding support for WTP version 2.0.
[INFO]
[INFO] --------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] --------------------------------------------------
This command will add the following files to your project:
.classpath
.project
.settings
target
You may have to enable "show hidden files" in your file explorer to view them
Open Eclipse and import the project
Conclusion
That's it! We've have successfully updated our Spring MVC application and exposed our repositories as RESTful endpoints using Spring Data Rest. We've also demonstrated how to access our application using pure hyperlinks with HATEOAS methodology.
I hope you've enjoyed this tutorial. Don't forget to check my other tutorials at the Tutorials section.