Before we start, please take some time to review our existing application: Spring MVC 3.1, jqGrid, and Spring Data JPA Integration Guide. If all you need is a pure Spring Data Rest application, please visit the official Spring Data Rest starter web application.
Table of Contents
Part 1: Configuration- Update the pom.xml
- Update the web.xml
- Spring Configurations
- Repositories
Dependencies
- Spring core 3.2.0.RC1
- Spring Data Rest 1.0.0.RC3
- Spring Data JPA 1.1.0.RELEASE
- jQuery 1.6.4
- jqGrid 4.3.1
- See pom.xml for details
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
<properties> | |
... | |
<spring.core.version>3.2.0.RC1</spring.core.version> | |
<spring.data.jpa.version>1.1.0.RELEASE</spring.data.jpa.version> | |
<spring.data.rest.version>1.0.0.RC3</spring.data.rest.version> | |
... | |
</properties> | |
... | |
<dependencies> | |
... | |
<!-- Spring Data Rest --> | |
<dependency> | |
<groupId>org.springframework.data</groupId> | |
<artifactId>spring-data-rest-webmvc</artifactId> | |
<version>${spring.data.rest.version}</version> | |
</dependency> | |
... | |
</dependencies> |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<web-app xmlns="http://java.sun.com/xml/ns/javaee" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" | |
version="2.5"> | |
<display-name>Spring Data Rest Tutorial</display-name> | |
<listener> | |
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> | |
</listener> | |
<servlet> | |
<servlet-name>spring</servlet-name> | |
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> | |
</servlet> | |
<servlet-mapping> | |
<servlet-name>spring</servlet-name> | |
<url-pattern>/</url-pattern> | |
</servlet-mapping> | |
<servlet> | |
<servlet-name>rest-exporter</servlet-name> | |
<servlet-class>org.springframework.data.rest.webmvc.RepositoryRestExporterServlet</servlet-class> | |
<load-on-startup>1</load-on-startup> | |
</servlet> | |
<servlet-mapping> | |
<servlet-name>rest-exporter</servlet-name> | |
<url-pattern>/api/*</url-pattern> | |
</servlet-mapping> | |
</web-app> |
Spring Configurations
We need to do three tasks:- Update the database name
- Create a spring-data-rest.xml
- Update the applicationContext.xml
Update the database name
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
# database properties | |
app.jdbc.driverClassName=com.mysql.jdbc.Driver | |
app.jdbc.url=jdbc\:mysql\://localhost/spring_data_rest_tutorial | |
app.jdbc.username=root | |
app.jdbc.password= |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:p="http://www.springframework.org/schema/p" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> | |
<bean id="baseUri" class="java.net.URI"> | |
<constructor-arg value="http://localhost:8080/spring-data-rest-tutorial/api"/> | |
</bean> | |
<bean id="config" class="org.springframework.data.rest.webmvc.RepositoryRestConfiguration" | |
p:jsonpParamName="callback" | |
p:jsonpOnErrParamName="errback" | |
p:baseUri-ref="baseUri"> | |
<property name="domainTypeToRepositoryMappings"> | |
<map key-type="java.lang.Class" value-type="java.lang.Class"> | |
<entry key="org.krams.domain.User" | |
value="org.krams.repository.UserRepository"/> | |
<entry key="org.krams.domain.Role" | |
value="org.krams.repository.RoleRepository"/> | |
</map> | |
</property> | |
</bean> | |
</beans> |
Update the applicationContext.xml
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
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:context="http://www.springframework.org/schema/context" | |
xmlns:p="http://www.springframework.org/schema/p" | |
xmlns:mvc="http://www.springframework.org/schema/mvc" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd | |
http://www.springframework.org/schema/context | |
http://www.springframework.org/schema/context/spring-context-3.1.xsd | |
http://www.springframework.org/schema/mvc | |
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> | |
... | |
<import resource="spring-data-rest.xml"/> | |
... | |
</beans> |
Repositories
We need to do two tasks:- Update UserRepository
- Create a new repository RoleRepository
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
package org.krams.repository; | |
import org.krams.domain.User; | |
import org.springframework.data.domain.Page; | |
import org.springframework.data.domain.Pageable; | |
import org.springframework.data.jpa.repository.JpaRepository; | |
import org.springframework.data.jpa.repository.Query; | |
import org.springframework.data.repository.query.Param; | |
public interface UserRepository extends JpaRepository<User, Long> { | |
User findByUsername(@Param("username") String username); | |
Page<User> findByUsernameLike(@Param("username") String username, Pageable pageable); | |
Page<User> findByFirstNameLike(@Param("firstName") String firstName, Pageable pageable); | |
Page<User> findByLastNameLike(@Param("lastName") String lastName, Pageable pageable); | |
Page<User> findByFirstNameLikeAndLastNameLike(@Param("firstName") String firstName, @Param("lastName") String lastName, Pageable pageable); | |
@Query("select u from user u where u.role.role = :role") | |
Page<User> findByRole(@Param("role") Integer role, Pageable pageable); | |
} |
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
package org.krams.repository; | |
import org.krams.domain.Role; | |
import org.springframework.data.jpa.repository.JpaRepository; | |
public interface RoleRepository extends JpaRepository<Role, Long> { | |
} |
0 komentar:
Post a Comment