For this tutorial, we start from a derby database. This database manage a bookstore (Book, Author, Reviews etc ..).
You can follow this tutorial with your own database.
If you want to start with the same db : https://dl.dropboxusercontent.com/u/52079610/BookstoreDB_v2_Derby10.9.zip
The database name is bookstore and user/pwd is root/admin.
- Project Creation
- You can start by create a new maven project or start on an existing one.
- The packaging of this project must be "war"
- The java compliance level must be 1.7
- Initialize Telosys Tools
- Open the properties window of your project
- In the left entries, select "Telosys Tools" and fill all project informations in "General", "Packages" and "Folders" tab (right side)
- Click on the "Apply" button (don't forget this step!)
- Click on "Init Telosys Tools" in the "General" tab => The "Telosys Tools" directory is now added in the project
- In the "download" tab, download from this Github repository the "spring-data-rest-templates-TT210"
- Click on "Download selected bundle(s)" and verify the checkbox "Install downloaded bundle(s)" is checked.
- After this, close the project properties window.
- In the project, you should now have the "spring-data-rest-templates-TT210" directory under "TelosysTools/templates"
- Update maven dependencies
- Under the "spring-data-rest-templates-TT210" directory, open the /misc/maven-pom.txt
- Copy and paste the dependencies to your project pom (if they are not already exist)
- For this example, we use a dependency for the Derby driver. Adapt it to your database if needed.
- Database repository generation
- Open the database configuration file : "Telosys Tools / databases.dbcfg"
- The database editor is displayed
- Add a new connection to your database
- Test the connection to get "Connection is OK"
- Click on "Meta-data" tab, fill the schema name and click on "Get tables" to see the tables of your database.
- If all is OK, click on "Generate repository" button to generate a new ".dbrep" file in the "TelosysTools" directory
- Generate the REST API
- Click on the "Bulk generation" tab
- In the entities list, select all entities you want to manage (be careful for links, all linked entities must be selected)
- Select the "spring-data-rest-templates-TT210" templates bundle in the dropdown list => all templates are now visible in right list
- Select all templates and select "copy static resources"
- Click on the "Generate" button
- You should have a successfull message
- The project source code should compile successfully
- The Spring configuration
- For this project, the spring configuration was performed in a java way.
- RestWebApplicationInitilizer : Servlet context initilization
- JpaRepositoryConfig : Database access configuration
- ApplicationConfig : Declaration of MessageSource, validators, converters ...
- The JPA Entities
- The JPA Entities were generated under the package you have enter in the Telosys Tools properties window.
- The Spring data JPA repositories
- The JPA Repositories were generated under the "repository" package
- Search methods for the Many-to-One relationships were have also been generated
- Converters
- Use for entity how have composite primary key
- Validators
- Each entity had an associated validator wich is executed before the update
You can deploy the project under your server or launch mvn tomcat7:run
To browse the resources, here are several ways to:
- Access URL from your web browser (http://localhost:8989/)
- Use Postman extension for Chrome
- Launch the spring rest-shell (see : https://github.com/spring-projects/rest-shell
Enjoy!