
If you don’t know Persevere yet, check out its impressive list of features (JSONPath/JSON-RPC/Comet support, etc…).
Here is a short tutorial to expose your MySQL tables through the persevere JSON REST interface.
- Download persevere and unzip it.
- Download the JDBC driver for MySQL and put the jar file into the “persevere/lib” folder.
- Create a new file in “persevere/WEB-INF/config” named “myDatabase.json”
- We will now configure the data source: (replace “myDatabase” and “myTable” by your database and table names.)
({
"id":"myDatabase.json",
"sources": [
{
"name":"myTable",
"sourceClass":"org.persvr.datasource.DatabaseTableDataSource",
"driver":"com.mysql.jdbc.Driver",
"connection":
"jdbc:mysql://localhost/myDatabase?user=myDbUser&pass=",
"table":"myTable",
"idColumn":"id",
"dataColumns": [
"lastname",
"firstname",
...and so on with your table columns
],
“schema”:{
“data”:{”$ref”:”../myTable/"}Don’t forget to change your database user name and password !
}
}
]
} - run persevere (”java -jar start.jar”) and visit the following pages:
- http://localhost:8080/myTable
- http://localhost:8080/myTable/
- http://localhost:8080/myTable/1
What to do next ?
- Add other tables: “sources” is an array !
- Use UTF8: Adds the following at the end of the connection string:
&useUnicode=true&characterEncoding=utf-8&mysqlEncoding=utf8 - Add methods to the class or to its prototype (take a look at WEB-INF/config/examples.json)
- Write a script to auto-generate the config file from a database structure (and post it on the persevere forum)
- Read the Persevere Javascript Client Documentation and start coding applications !
Have fun !