...
- Install the MySQL server.
Download the JDBC driver for MySQL from here and copy it to your
<EI_HOME>/lib
directory.Note If the driver class does not exist in the relevant folders when you create the datasource, you will get an exception such as 'Cannot load JDBC driver class com.mysql.jdbc.Driver'.- Create the following database:
Employees
Create the Employee table inside the Employees database:
Code Block CREATE TABLE Employees (EmployeeNumber int(11) NOT NULL, FirstName varchar(255) NOT NULL, LastName varchar(255) DEFAULT NULL, Email varchar(255) DEFAULT NULL, Salary varchar(255));
...
GET data
...
using JSON
...
A data service can expose data in one of the following formats: XML, RDF, or JSON. You can select the required format by specifying the output type for the data service query. To expose data in JSON, you need to select JSON as the output type, and map the output to a JSON template.
...
- Log in to the product's management console and upload the data service.
- Click Add → Data Service → Upload.
- Browse and add the RDBMSDataService.dbs file.
- Click List under Main → Services. The RDBMS data service should be listed.
- Click the data service to open the Service Dashboard.
- Click Edit Data Service (Wizard) to open the data service using the Create Data Service wizard.
- Click Next until you get to the Queries screen.
- Edit the GetEmployeeDetails query.
- Change the Output Type to JSON.
You can now start defining the JSON template for the output. Listed below are a few sample templates that you can use for this query.
Shown below is a basic mapping.
Code Block language javascript title Sample JSON Mapping { "Employees": {"Employee":[ {"EmployeeNumber":"$EmployeeNumber", "Details": { "FirstName":"$FirstName", "LastName":"$LastName", "Email":"$Email", "Salary":"$Salary" } } ] } }
Tip Note the following:
As shown in the sample given above, the column name values that are expected in the query result should be referred to by the column name with the "$" prefix. E.g. "$EmployeeNumber".
Also, the structure of the JSON template should follow some guidelines in order to be compatible with the result. These guidelines are:
- The top most item should be a JSON object. It cannot be a JSON array.
- For handling multiple records from the result set, the immediate child of the top most object can be a JSON array, and the array should contain only a single object.
- If only a single result is returned, the immediate child of the top most object can be a single JSON object.
- After the immediate child of the top most object, there cannot be other JSON arrays in the mapping.
All JSON responses will be returned as an array.
In a basic JSON output mapping, we specify the field values that we expect in the query result. You can give additional properties to this field mapping such as data type of the field, the possible content filtering user roles etc. These extended properties for the fields are given in parentheses, with a list of string tokens providing the additional properties, separated by a semicolon (";"). See the sample below.
Code Block language javascript title Sample JSON Mapping { "Employees": {"Employee":[ {"EmployeeNumber":"$EmployeeNumber(type:integer)", "Details": { "FirstName":"$FirstName", "LastName":"$LastName", "Email":"$Email", "Salary":"$Salary(requiredRoles:hr,admin)" } } ] } }
Tip Setting data types
- The 'data type' property is added to the 'EmployeeNumber' field using parentheses "()".
- The extended property 'type' is given along with the value 'integer'.
- The property and the value are separated by a colon.
- Note that the possible values for data type are "integer", "long", "double", and "boolean".
Content filtering (Required Roles)
- The 'data type' extended property, as well as the 'required roles' extended property, is added to the 'Salary' field using parentheses "()".
- The 'requiredRoles' property is added along with the values 'hr' and 'admin'.
- Note that the two values for the 'requiredRoles' property are separated by a comma.
- Also, the two extended properties are separated by a semicolon.
If you want to write a nested query using JSON, see the tutorial on working with nested queries.
- Save the query.
Get data in JSON
The RDBMSDataService that you are using will already contain the following resource defined:
...
You will receive the response in JSON format.
...
POST/UPDATE data using JSON
When a client sends a request to change data (POST/PUT/DELETE) in the datasource, the HTTP header "Accept" should be set to "application/json". Also, if the data is sent as a JSON payload, the HTTP header "Content-Type" should be set to "application/json".
...
Tip | ||||
---|---|---|---|---|
| ||||
Note the following when you define a JSON payload: The object name specified in the payload must be in the following format: "
However, if the Handling a resource path with the "/" symbolIf the
For example, if the
The child name/values of the child fields in the payload should be the names and values of the input parameters in the target query.If you are sending a batch request to the "/employee/add" resource, the payload object name should be as follows:
|