Sunday, March 2, 2014

Exporting Spring Data JPA Repositories as REST Services using Spring Data REST

Spring Data modules provides various modules to work with various types of datasources like RDBMS, NOSQL stores etc in unified way. In my previous article  SpringMVC4 + Spring Data JPA + SpringSecurity configuration using JavaConfig I have explained how to configure Spring Data JPA using JavaConfig.

Now in this post let us see how we can use Spring Data JPA repositories and export JPA entities as REST endpoints using Spring Data REST.

First let us configure spring-data-jpa and spring-data-rest-webmvc dependencies in our pom.xml.
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.5.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-webmvc</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>


Make sure you have latest released versions configured correctly, otherwise you will encounter the following error:
java.lang.ClassNotFoundException: org.springframework.data.mapping.SimplePropertyHandler

Create JPA entities.
@Entity
@Table(name = "USERS")
public class User implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
private Integer id;
@Column(name = "username", nullable = false, unique = true, length = 50)
private String userName;
@Column(name = "password", nullable = false, length = 50)
private String password;
@Column(name = "firstname", nullable = false, length = 50)
private String firstName;
@Column(name = "lastname", length = 50)
private String lastName;
@Column(name = "email", nullable = false, unique = true, length = 50)
private String email;
@Temporal(TemporalType.DATE)
private Date dob;
private boolean enabled=true;
@OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
@JoinColumn(name="user_id")
private Set<Role> roles = new HashSet<>();
@OneToMany(mappedBy = "user")
private List<Contact> contacts = new ArrayList<>();
//setters and getters
}
@Entity
@Table(name = "ROLES")
public class Role implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "role_id")
private Integer id;
@Column(name="role_name",nullable=false)
private String roleName;
//setters and getters
}
@Entity
@Table(name = "CONTACTS")
public class Contact implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "contact_id")
private Integer id;
@Column(name = "firstname", nullable = false, length = 50)
private String firstName;
@Column(name = "lastname", length = 50)
private String lastName;
@Column(name = "email", nullable = false, unique = true, length = 50)
private String email;
@Temporal(TemporalType.DATE)
private Date dob;
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
//setters and getters
}


Configure DispatcherServlet using AbstractAnnotationConfigDispatcherServletInitializer.

Observe that we have added RepositoryRestMvcConfiguration.class to getServletConfigClasses() method.
RepositoryRestMvcConfiguration is the one which does the heavy lifting of looking for Spring Data Repositories and exporting them as REST endpoints.
package com.sivalabs.springdatarest.web.config;
import javax.servlet.Filter;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
import org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import com.sivalabs.springdatarest.config.AppConfig;
public class SpringWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
{
@Override
protected Class<?>[] getRootConfigClasses()
{
return new Class<?>[] { AppConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses()
{
return new Class<?>[] { WebMvcConfig.class, RepositoryRestMvcConfiguration.class };
}
@Override
protected String[] getServletMappings()
{
return new String[] { "/rest/*" };
}
@Override
protected Filter[] getServletFilters() {
return new Filter[]{
new OpenEntityManagerInViewFilter()
};
}
}



Create Spring Data JPA repositories for JPA entities.
public interface UserRepository extends JpaRepository<User, Integer>
{
}
public interface RoleRepository extends JpaRepository<Role, Integer>
{
}
public interface ContactRepository extends JpaRepository<Contact, Integer>
{
}


That's it. Spring Data REST will take care of rest of the things.

You can use spring Rest Shell https://github.com/spring-projects/rest-shell or Chrome's Postman Addon to test the exported REST services.

D:\rest-shell-1.2.1.RELEASE\bin>rest-shell
http://localhost:8080:>

Now we can change the baseUri using baseUri command as follows:
http://localhost:8080:>baseUri http://localhost:8080/spring-data-rest-demo/rest/
http://localhost:8080/spring-data-rest-demo/rest/>


http://localhost:8080/spring-data-rest-demo/rest/>list
rel         href
======================================================================================
users       http://localhost:8080/spring-data-rest-demo/rest/users{?page,size,sort}
roles       http://localhost:8080/spring-data-rest-demo/rest/roles{?page,size,sort}
contacts    http://localhost:8080/spring-data-rest-demo/rest/contacts{?page,size,sort}

Note: It seems there is an issue with rest-shell when the DispatcherServlet url mapped to "/" and issue list command it responds with "No resources found".

http://localhost:8080/spring-data-rest-demo/rest/>get users/
{
"_links": {
"self": {
"href": "http://localhost:8080/spring-data-rest-demo/rest/users/{?page,size,sort}",
"templated": true
},
"search": {
"href": "http://localhost:8080/spring-data-rest-demo/rest/users/search"
}
},
"_embedded": {
"users": [
{
"userName": "admin",
"password": "admin",
"firstName": "Administrator",
"lastName": null,
"email": "admin@gmail.com",
"dob": null,
"enabled": true,
"_links": {
"self": {
"href": "http://localhost:8080/spring-data-rest-demo/rest/users/1"
},
"roles": {
"href": "http://localhost:8080/spring-data-rest-demo/rest/users/1/roles"
},
"contacts": {
"href": "http://localhost:8080/spring-data-rest-demo/rest/users/1/contacts"
}
}
},
{
"userName": "siva",
"password": "siva",
"firstName": "Siva",
"lastName": null,
"email": "sivaprasadreddy.k@gmail.com",
"dob": null,
"enabled": true,
"_links": {
"self": {
"href": "http://localhost:8080/spring-data-rest-demo/rest/users/2"
},
"roles": {
"href": "http://localhost:8080/spring-data-rest-demo/rest/users/2/roles"
},
"contacts": {
"href": "http://localhost:8080/spring-data-rest-demo/rest/users/2/contacts"
}
}
}
]
},
"page": {
"size": 20,
"totalElements": 2,
"totalPages": 1,
"number": 0
}
}
view raw response.json hosted with ❤ by GitHub

5 comments:

  1. Hello Nice Tutorial,

    However could you show us how do you manage date format and supporting i18n?

    Thank you

    ReplyDelete
    Replies
    1. Here I used Jackson json library to marshalling/unmarshalling objects to/from json. So to customize Date formats we need to register Custom DateSerializer, Desirializer. For i18n we are just getting data from DB and converting it into JSON, no additional config is required if your data in DB is in desired language.

      Delete
  2. Thank you Siva,
    I found an example solution on http://loianegroner.com/2010/09/how-to-serialize-java-util-date-with-jackson-json-processor-spring-3-0/ however this should be improved. It's not the best and I believe it should be improved by setting the formatter based on the Locale of the client.
    Now I am trying to solve another problem related to the uniqueness of an entity on a field. http://stackoverflow.com/questions/3495368/unique-constraint-with-jpa-and-bean-validation/3499111#3499111 I found how to solve it with jboss http://lucasterdev.altervista.org/wordpress/2012/07/28/unique-constraint-validation-in-jpa-part-2/ but I would like to write less code.

    ReplyDelete
  3. Hi Siva

    Above json output how to convert it to user object and above json data dosent contain the roles and contact information. can you pls explain

    ReplyDelete
  4. Hi Siva

    please resolve this
    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:943)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
    com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
    org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:177)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

    ReplyDelete