Added spec tests, README.
This commit is contained in:
19
README.md
Normal file
19
README.md
Normal file
@@ -0,0 +1,19 @@
|
||||
# Spring Data Rest Exporter
|
||||
|
||||
The Spring Data Rest exporter is a project that aims to make it easy to expose various
|
||||
services as Rest endpoints. The goal of the project is to provide a flexible and configurable
|
||||
mechanism for writing simple services that can expose arbitrary services over HTTP.
|
||||
|
||||
The first exporter implemented is a JPA Repository exporter. This takes your JPA repositories
|
||||
and front-ends them with HTTP, allowing you full CRUD capability over your entities, to include
|
||||
managing associations.
|
||||
|
||||
### Installation
|
||||
|
||||
To use the Spring Data Rest exporter, first package your domain classes and repositories into a JAR
|
||||
file. Include some Spring XML configuration files in the `META-INF/spring-data-rest` directory in
|
||||
that JAR file (including an applicable EntityManager and DataSource).
|
||||
|
||||
You can either deploy this JAR file into your Servlet container in a "shared" configuration, or you
|
||||
can add this JAR file (and any other application dependencies to the exporter WAR file's `WEB-INF/lib`
|
||||
directory.
|
||||
@@ -1,3 +1,5 @@
|
||||
archivesBaseName = "spring-data-rest-core"
|
||||
|
||||
dependencies {
|
||||
|
||||
// Google Guava
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.springframework.data.rest.core
|
||||
package org.springframework.data.rest.core.spec
|
||||
|
||||
import org.springframework.data.rest.core.util.UriUtils
|
||||
import spock.lang.Specification
|
||||
@@ -1,3 +1,5 @@
|
||||
archivesBaseName = "spring-data-rest-repository"
|
||||
|
||||
dependencies {
|
||||
|
||||
// Spring
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package org.springframework.data.rest.repository.spec
|
||||
|
||||
import javax.persistence.Entity
|
||||
import javax.persistence.EntityManager
|
||||
import javax.persistence.GeneratedValue
|
||||
import javax.persistence.Id
|
||||
import javax.persistence.PersistenceContext
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.context.ApplicationContext
|
||||
import org.springframework.data.repository.CrudRepository
|
||||
import org.springframework.data.rest.repository.JpaRepositoryMetadata
|
||||
import org.springframework.test.context.ContextConfiguration
|
||||
import spock.lang.Specification
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin <jon@jbrisbin.com>
|
||||
*/
|
||||
@ContextConfiguration(locations = ["/JpaMetadataSpec-test.xml"])
|
||||
class JpaMetadataSpec extends Specification {
|
||||
|
||||
@Autowired
|
||||
ApplicationContext applicationContext
|
||||
@PersistenceContext
|
||||
EntityManager entityManager
|
||||
@Autowired
|
||||
Collection<CrudRepository> repositories
|
||||
JpaRepositoryMetadata repoMeta
|
||||
|
||||
def setup() {
|
||||
repoMeta = new JpaRepositoryMetadata(
|
||||
repositories: repositories,
|
||||
applicationContext: applicationContext,
|
||||
entityManager: entityManager
|
||||
)
|
||||
repoMeta.afterPropertiesSet()
|
||||
}
|
||||
|
||||
def "finds repositories in ApplicationContext"() {
|
||||
|
||||
when:
|
||||
def repo = repoMeta.repositoryFor("simple")
|
||||
|
||||
then:
|
||||
null != repo
|
||||
repo instanceof SimpleRepository
|
||||
|
||||
when:
|
||||
repo = repoMeta.repositoryFor(Simple)
|
||||
|
||||
then:
|
||||
null != repo
|
||||
repo instanceof SimpleRepository
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Entity
|
||||
class Simple {
|
||||
@Id @GeneratedValue Long id
|
||||
String name
|
||||
}
|
||||
|
||||
interface SimpleRepository extends CrudRepository<Simple, Long> {}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
|
||||
<persistence-unit name="jpa.sample">
|
||||
<class>org.springframework.data.rest.repository.spec.Simple</class>
|
||||
<properties>
|
||||
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
|
||||
<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:spring"/>
|
||||
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
|
||||
<property name="hibernate.connection.username" value="sa"/>
|
||||
<property name="hibernate.connection.password" value=""/>
|
||||
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
</persistence>
|
||||
26
repository/src/test/resources/JpaMetadataSpec-test.xml
Normal file
26
repository/src/test/resources/JpaMetadataSpec-test.xml
Normal file
@@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
|
||||
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
|
||||
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
|
||||
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
|
||||
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd">
|
||||
|
||||
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
<property name="jpaVendorAdapter">
|
||||
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
|
||||
<property name="generateDdl" value="true"/>
|
||||
<property name="database" value="HSQL"/>
|
||||
</bean>
|
||||
</property>
|
||||
<property name="persistenceUnitName" value="jpa.sample"/>
|
||||
<property name="persistenceXmlLocation" value="/JpaMetadataSpec-persistence.xml"/>
|
||||
</bean>
|
||||
|
||||
<jdbc:embedded-database id="dataSource" type="HSQL"/>
|
||||
|
||||
<jpa:repositories base-package="org.springframework.data.rest.repository.spec"/>
|
||||
|
||||
</beans>
|
||||
@@ -1,6 +1,8 @@
|
||||
apply plugin: "war"
|
||||
apply plugin: "jetty"
|
||||
|
||||
archivesBaseName = "spring-data-rest"
|
||||
|
||||
dependencies {
|
||||
|
||||
// APIS
|
||||
|
||||
@@ -9,6 +9,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.ImportResource;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.data.rest.repository.JpaRepositoryMetadata;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
@@ -27,6 +29,8 @@ public class RepositoryRestConfiguration {
|
||||
@Autowired(required = false)
|
||||
JpaRepositoryMetadata jpaRepositoryMetadata;
|
||||
@Autowired(required = false)
|
||||
ConversionService conversionService;
|
||||
@Autowired(required = false)
|
||||
List<HttpMessageConverter<?>> httpMessageConverters = new ArrayList<HttpMessageConverter<?>>();
|
||||
|
||||
@Bean URI baseUri() {
|
||||
@@ -36,6 +40,13 @@ public class RepositoryRestConfiguration {
|
||||
return baseUri;
|
||||
}
|
||||
|
||||
@Bean ConversionService conversionService() {
|
||||
if (null == conversionService) {
|
||||
conversionService = new DefaultConversionService();
|
||||
}
|
||||
return conversionService;
|
||||
}
|
||||
|
||||
@Bean List<HttpMessageConverter<?>> httpMessageConverters() {
|
||||
if (httpMessageConverters.isEmpty()) {
|
||||
MappingJacksonHttpMessageConverter json = new MappingJacksonHttpMessageConverter();
|
||||
|
||||
@@ -342,7 +342,7 @@ public class RepositoryRestController implements InitializingBean {
|
||||
model.addAttribute(RESOURCE, child);
|
||||
}
|
||||
}
|
||||
} else if (uriCnt == 4) {
|
||||
} else {
|
||||
final String childId = UriUtils.path(uris.get(3));
|
||||
Class<? extends Serializable> childIdType = childEntityInfo.getIdType();
|
||||
final Serializable childSerId;
|
||||
|
||||
@@ -48,6 +48,7 @@ public class RepositoryRestMvcConfiguration {
|
||||
this.repositoryRestController = new RepositoryRestController()
|
||||
.baseUri(repositoryRestConfiguration.baseUri())
|
||||
.repositoryMetadata(repositoryRestConfiguration.jpaRepositoryMetadata())
|
||||
.conversionService(repositoryRestConfiguration.conversionService())
|
||||
.httpMessageConverters(repositoryRestConfiguration.httpMessageConverters())
|
||||
.jsonMediaType("application/json");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user