Provides a REST API for a Library (one that has books)
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.settings
src
.classpath
.gitignore
.project
.travis.yml
README.md
build.xml
pom.xml

README.md

Library API

Build Status

Provides a REST API for a mock (book) library

Overview

This project was an excuse to build a Java REST based application using Spring. The application provides an API around the mechanics of a library, but currently only contains the Book resource. More to be added...

Technical Details

This uses Spring's AbstractAnnotationConfigDispatcherServletInitializer to allow for a web.xml-less web application project. All configuration that would be stored within the web.xml is instead stored within the org.library.WebApplicationInitializer. Spring Beans are provided within @Configuration classes instead of xml files. The project uses Spring Profiles to dynamically load the configuration based on the environment. For example, in the development environment the org.library.config.data.DevelopmentDataConfig is loaded which uses an embedded DataSource and populates the database with a preconfigured set of books found in the data.json file. However in the production environment the org.library.config.data.ProductionDataConfig is loaded which uses JNDI to retrieve the DataSource and does not pre-populate the database.

Spring Data is used to configure and load the database, as well as the entities and repositories. For example, the org.library.repository.BookRepository references the org.library.domain.Book entity. The entities extend from an internal org.library.domain.AbstractPersistable which provides its own ID generator: org.library.persistence.UseExistingOrGenerateIdGenerator. This was necessary to use IDs specified in the data.json data file used to pre-populate the database, but also generate unique IDs when the ID is not specified. The repository interfaces extend from Spring Data to automatically provide default CRUD operations, as well as paging and sorting functions.

Validation is done at the API layer to enforce correct data on POST or PUT operations. This uses a combination of JSR-303 and Hibernate to provide validation. Annotations are added to the entity class to declare the requirements and the @Valid annotation is added to the bean itself in the controllers. A MessageSource is defined with error messages stored in messages/errors.properties. Because the messages were not supplied by default, an exception resolver was added to use the message in the response error message if available (org.library.config.exception.IncludeMessageSourceExceptionResolver).

Both unit and integration tests were provided. For integration tests, Spring Test was used to load up the configuration prior to running the tests. org.springframework.test.web.servlet.MockMvc was used to send mock requests to test the REST APIs. Travis-CI integration is also implemented, with tests running against Oracle JDK 7 and Open JDK 7.

Getting Started

Clone the project, run mvn compile test-compile to download dependencies and compile the project. Java 7 is required. To run the tests, execute mvn test.

An Ant build file was created to simplify starting the server to run the application. Run ant tomcat to start the application using an embedded Tomcat 7 server (and embedded HSQL database).