What is spring-data-rest?
spring-data-rest, a recent addition to the spring-data project, is a framework that helps you expose your entities directly as RESTful webservice endpoints. Unlike rails, grails or roo it does not generate any code achieving this goal. spring data-rest supports JPA, MongoDB, JSR-303 validation, HAL and many more. It is really innovative and lets you setup your RESTful webservice within minutes. In this example i'll give you a short overview of what spring-data-rest is capable of.
Initial Configuration
I'm gonna use the new Servlet 3 Java Web Configuration instead of an ancient web.xml. Nothing really special here.public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[]{AppConfiguration.class}; } @Override protected Class<?>[] getServletConfigClasses() { return new Class[]{WebConfiguration.class}; } @Override protected String[] getServletMappings() { return new String[]{"/"}; } }
I am initializing hibernate as my database abstraction layer in the AppConfiguration class. I'm using an embedded database (hsql), since i want to keep this showcase simple stupid. Still, nothing special here.
@Configuration @EnableJpaRepositories @EnableTransactionManagement public class AppConfiguration { @Bean public DataSource dataSource() { EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); return builder.setType(EmbeddedDatabaseType.HSQL).build(); } @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); vendorAdapter.setDatabase(Database.HSQL); vendorAdapter.setGenerateDdl(true); LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setJpaVendorAdapter(vendorAdapter); factory.setPackagesToScan(getClass().getPackage().getName()); factory.setDataSource(dataSource()); return factory; } @Bean public PlatformTransactionManager transactionManager() { return new JpaTransactionManager(); } }
Now to the application servlet configuration: WebConfiguration
@Configuration public class WebConfiguration extends RepositoryRestMvcConfiguration { }
Oh, well thats a bit short isnt it? Not a single line of code required for a complete setup. This is a really nice application of the convention over configuration paradigm. We can now start creating spring-data-jpa repositories and they will be exposed as RESTful resources automatically. And we can still add custom configuration to the WebConfiguration class later if needed.
The Initialization was really short and easy. We didn't have to code anything special. The only thing we did was setting up a database connection and hibernate, which is obviously inevitable. Now, that we have setup our "REST Servlet" and persistence, lets move on to the application itself, starting with the model.
The Model
I'll keep it really simple creating only two related entities.@Entity public class Book { @Id private String isbn; private String title; private String language; @ManyToMany private List<Author> authors; }
@Entity public class Author { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE) private Integer id; private String name; @ManyToMany(mappedBy = "authors") private List<Book> books; }
To finally make the entities persistent and exposed as a RESTful webservice, we need spring-data repositories. A repository is basically a DAO. It offers CRUD functionality for our entities. spring-data takes away most of your programming effort creating such repositories. We just have to define an empty interface, spring-data does everything else out of the box. Still, it is easily customizable thanks to its design by convention over configuration!
The Actual Repositories
@RestResource(path = "books", rel = "books") public interface BookRepository extends PagingAndSortingRepository<Book, Long> { }
@RestResource(path = "authors", rel = "authors") public interface AuthorRepository extends PagingAndSortingRepository<Author, Integer> { }
Again, there is nearly no code needed. Even the @RestResource annotation could be left out. But if i did, the path and rel would be named after the entity, which i dont want. A REST resource that contains multiple children should be named plural though.
Accessing The Result
Our RESTful webservice is now ready for deployment. Once run, it lists all available resources on the root, so you can navigate from there.GET http://localhost:8080/
{ "links" : [ { "rel" : "books", "href" : "http://localhost:8080/books" }, { "rel" : "authors", "href" : "http://localhost:8080/authors" } ], "content" : [ ] }
Fine! Now lets create an author and a book.
POST http://localhost:8080/authors
{"name":"Uncle Bob"}
Response
201 Created Location: http://localhost:8080/authors/1PUT http://localhost:8080/books/0132350882
{ "title": "Clean Code", "authors": [ { "rel": "authors", "href": "http://localhost:8080/authors/1" } ] }
Response
201 Created
Noticed how i used PUT to create the book? This is because its id is the actual isbn. I have to tell the server which isbn to use since he cant guess it. I used POST for the author as his id is just an incremental number that is generated automatically. Also, i used a link to connect both, the book (/books/0132350882) and the author (/authors/1). This is basically what hypermedia is all about: Links are used for navigation and relations between entities.Now, lets see if the book was created accordingly.
GET http://localhost:8080/books
{ "links" : [ ], "content" : [ { "links" : [ { "rel" : "books.Book.authors", "href" : "http://localhost:8080/books/0132350882/authors" }, { "rel" : "self", "href" : "http://localhost:8080/books/0132350882" } ], "title" : "Clean Code" } ], "page" : { "size" : 20, "totalElements" : 1, "totalPages" : 1, "number" : 1 } }
Fine!
Here is an Integration Test, following these steps automatically. It is also available in the example on github.
public class BookApiIT { private final RestTemplate restTemplate = new RestTemplate(); private final String authorsUrl = "http://localhost:8080/authors"; private final String booksUrl = "http://localhost:8080/books"; @Test public void testCreateBookWithAuthor() throws Exception { final URI authorUri = restTemplate.postForLocation(authorsUrl, sampleAuthor()); // create Author final URI bookUri = new URI(booksUrl + "/" + sampleBookIsbn); restTemplate.put(bookUri, sampleBook(authorUri.toString())); // create Book linked to Author Resource<Book> book = getBook(bookUri); assertNotNull(book); final URI authorsOfBookUri = new URI(book.getLink("books.Book.authors").getHref()); Resource<List<Resource<Author>>> authors = getAuthors(authorsOfBookUri); assertNotNull(authors.getContent()); assertFalse(authors.getContent().isEmpty()); // check if /books/0132350882/authors contains an author } private String sampleAuthor() { return "{\"name\":\"Robert C. Martin\"}"; } private final String sampleBookIsbn = "0132350882"; private String sampleBook(String authorUrl) { return "{\"title\":\"Clean Code\",\"authors\":[{\"rel\":\"authors\",\"href\":\"" + authorUrl + "\"}]}"; } private Resource<Book> getBook(URI uri) { return restTemplate.exchange(uri, HttpMethod.GET, null, new ParameterizedTypeReference<Resource<Book>>() { }).getBody(); } private Resource<List<Resource<Author>>> getAuthors(URI uri) { return restTemplate.exchange(uri, HttpMethod.GET, null, new ParameterizedTypeReference<Resource<List<Resource<Author>>>>() { }).getBody(); } }
Conclusion
We have created a complete RESTful webservice without much coding effort. We just defined our entities and database connection. spring-data-rest stated that everything else is just boilerplate, and i agree.To consume the webservices manually, consider the rest-shell. It is a command-shell making the navigation in your webservice as easy and fun as it could be. Here is a screenshot:
The complete example is available on my github
https://github.com/gregorriegler/babdev-spring/tree/master/spring-data-rest
https://github.com/gregorriegler/babdev-spring
Nice tutorial. Out of curiosity, have you ended up building a web client on top of a SpringData-REST app?
ReplyDeleteI ask because I love what I've seen & played with from SD-Rest, but I haven't figured out a clean way to "add" a SpringMVC app on top of it. You'd think it'd be as easy as registering controllers (or possibly secondary dispatcher servlet) but once I went down those roads things started crashing.
not just yet. but i have built webapps on top of similar rest web services. if you configure data-rest to use json-hal you should be able to attach backbone to it using backbone-hal. this way you got your entities in javascript without having to worry about http. id suggest building your web client using javascript
Delete