REST 2.0 service binding

Use the REST 2.0 binding to access an information service through a simple HTTP interface and generate a response in either XML or JSON format. IBM® InfoSphere® Information Services Director supports GET, POST, PUT, and DELETE actions for the REST 2.0 binding.

When you attach a REST 2.0 binding, settings must be configured at the services level and operation level.

The REST 2.0 binding contains the following settings:

Input argument encoding

The encoding of the service operation input arguments is driven by two operation level REST 2.0 properties: Parameter Type and Format.

Parameter Type specifies which part of the HTTP request will hold the input arguments. Depending on the Parameter Type value, the Format value might then be used to further specify how the input arguments must be encoded. Valid values for Parameter Type are: URLQUERY, URLEXTENDED, BODY or MULTIPART.
URLQUERY
URLQUERY is supported only for GET and DELETE HTTP methods and only if all the service input arguments are scalar values (integer, float, boolean, string, etc). The service input arguments must be encoded in the URL query string using the standard URL parameter encoding mechanism:

http://servername:port/wisd-rest2/<application-name>/<OperationContext>?arg1=value1&arg2=value2

The name of the URL parameters (arg1, arg2 in the example above) must match the name of the corresponding operation input argument.

URLEXTENDED
URLEXTENDED is supported only for GET and DELETE HTTP methods and only if all the service input arguments are scalar values (integer, float, boolean, string, etc). The service input arguments must be provided as part of the URL extended path:

http://servername:port/wisd-rest2/<application-name>/<OperationContext>/arg1Value/arg2Value

As the argument names are not specified, the arguments must be provided in the same order as defined in the operation signature.

BODY
BODY is supported only for POST and PUT HTTP methods and for any kind (scalar or complex) and number of service input arguments. The service input arguments must be provided in the HTTP request body and encoded in JSON or XML according to the Format value. See XML format and JSON format, below.
MULTIPART
MULTIPART is supported only for POST and PUT HTTP methods and for service operations with scalar only input arguments. The HTTP request must provide one part for each input argument with a name – name attribute of the Content-Dispostion header of the part, for example, Content-Disposition: form-data; name="arg1" – matching the name of the corresponding operation input argument. Each part must contain the scalar value “as is” (no XML or JSON encoding).

Input argument format

The operation-level Format property that you configure in REST 2.0 Settings specifies the input argument encoding format for HTTP POST and PUT requests with a BODY parameter type as the arguments must be provided as part of the HTTP body. See Operation Bindings Default Settings, above.
XML
If XML is used, the input arguments must be XML encoded and supplied in the HTTP request body.
JSON
If JSON is used, the input arguments must be JSON encoded and supplied in the HTTP request body.
DYNAMIC
If Dynamic is used, the format can be different at each service invocation. The input arguments must be encoded in XML or JSON following the same rule as defined in the XML Format and JSON Format sections and the Content-Type header of the HTTP request must be set accordingly to application/xml or application/json. The REST 2.0 binding uses the Content-Type header value to determine the expected encoding format.

Output argument encoding

The encoding of the service operation output argument is driven by the operation level Format property. See Operation Bindings Default Settings, above. The output argument encoding is independent from the HTTP method that is used for the HTTP request (GET, POST, PUT or DELETE). The encoded output argument is supplied as part of the HTTP response body.

Output argument Format values

The operation-level Format property you configure in REST 2.0 Settings specifies the output arguments encoding format. Valid values for Format are: XML, JSON, and DYNAMIC.
XML
If XML is used, the output argument is XML-encoded and returned in the HTTP response body. The HTTP response Content-Type header is also set to application/xml.
JSON
If JSON is used, the output argument is JSON-encoded and returned in the HTTP response body. The HTTP response Content-Type header is also set to application/json.
DYNAMIC
If Dynamic is used, the client must include the application/xml or application/json content type in the Accept header of the HTTP request. The REST 2.0 binding uses the Accept header value to figure out how the HTTP response must be encoded and to properly set the Content-Type header. If the Accept header is missing or does not contain a valid content type (either application/xml or application/json), an HTTP Status 400® error is returned.

HTTP methods

Each service operation is associated with only one HTTP method. Clients must use the specified HTTP method in order to be able to call the corresponding service operation.

Due to the parameter encoding limitations of the GET and DELETE HTTP methods (parameters must be encoded as part of the URL), these methods can only be used for service operation without input arguments or with only scalar input arguments.

For service operations associated with PUT and DELETE HTTP methods, clients can use an HTTP POST to invoke these operations providing that they set the X-HTTP-Method-Override header of the HTTP request to the intended HTTP method (PUT or DELETE). This PUT/DELETE tunneling mechanism through POST is always enabled. For any HTTP POST request, if the X-HTTP-Method-Override header is set, then the HTTP method overwrite will take place. If the X-HTTP-Method-Override header is set to something other than PUT or DELETE, an HTTP 400 error (SC_BAD_REQUEST) is returned.

Calling the service by using REST

After you deploy your service, the service can be called by using the proper URL format and argument encoding.

URL Format

Each service operation is uniquely identified through the REST 2.0 binding by its URL and the HTTP method it is associated with. By default, the URL used to invoke each service operation is as follows:
http://servername:port/wisd-rest2/<application-name>/<OperationContext>
Note: OperationContext defaults to <service-name>/<operation-name> unless overwritten in the REST 2.0 Settings operation binding properties
XML format
Service requests encoded in XML are composed as follows:
  • An XML root element enclosing all the input arguments (if any). The name of this root element can be anything. The REST 2.0 binding does not enforce any constraints on the name of the root element.
  • Each input arguments (if any) are encoded using an XML subelement of the root element and named after the name of one of the input arguments of the operation.
Service responses encoded in XML are composed as follows:
  • An XML root element called “results” directly containing the operation returned value.
  • If the service operation does not return anything (void), an empty XML results element is used.
  • The operation returned values is directly enclosed in the results root element. For example, if the operation returns a scalar value, an XML text node with that value is directly enclosed under the results root element.
Scalar data types
Scalar data types, or Java™ primitive types, are all encoded as XML text nodes (strings in the XML document). The value of the XML text node is the value returned by the Java toString() method for each of the primitive types. The only exception is for the java.util.Calendar type: the text node value is the date formatted in the ISO 8601 format: yyyy-MM-dd'T'HH:mm:ss.SSSZ.
Complex data types
For complex data types, a JavaBean instance is encoded into an XML chunk with each property (attribute) of the JavaBean encoded as an XML element in that chunk and named after the JavaBean property name.
Arrays
Java arrays are encoded as XML unbounded sequences. By default, there is enclosing XML element generated to hold all the array items. Array items are encoded like any other non array JavaBean attribute or service operation input argument, except that there is one XML element for each item of the array.
JSON format
Service requests encoded in JSON are composed of a single JSON object with one attribute for each service operation input argument. The name of these attributes must match the name of one of the input arguments of the operation. If the service operation does not have any input arguments, an empty JSON object must be provided: {}.
Service responses encoded in JSON are composed of a single JSON object with a single attribute called “results” holding the service operation returned value. If the service operation returns void, a null results JSON object is returned: {“results”:null}.
Scalar data types
The following table shows the list of supported scalar data types and the corresponding JSON format.
Table 1. Scalar data types and JSON format
Scalar Data Type JSON Format
boolean true or false
byte Number
double Number
float Number
int Number
java.lang.Boolean true or false
java.lang.Byte Number
java.lang.Double Number
java.lang.Float Number
java.lang.Integer Number
java.lang.Long Number
java.lang.Short Number
java.lang.String String
java.math.BigDecimal Number
java.math.BigInteger Number
java.util.Calendar Object representing a JavaScript Calendar: {"date":26,"day":4,"hours":15,"minutes":38, "month":1,"seconds":6, "time":1235659086302, "timezoneOffset":-60,"year":109}
long Number
short Number
Complex data type
A JavaBean instance is encoded into a JSON object with each property (attribute) of the JavaBean encoded as an attribute of the JSON object and named after the JavaBean property name.
Arrays
Java arrays are encoded as JSON arrays. Each element of the array is encoded as specified in Table 1, above. There is one exception to this rule for byte arrays (byte[]). In this case, the byte array is base64-encoded and is handled as a JSON String.

Error handling

By default, if any Java exception is thrown by the service operation being invoked, the REST 2.0 binding returns an HTTP 500 (Internal Server Error) response with the exception details encoded in the HTTP response body in XML or JSON format.

An example XML exception is:

<?xml version="1.0" encoding="UTF-8"?>
<exception xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:java="http://java.sun.com">
<errorcode>[error code]</errorcode>
<stacktrace>[The exception stack trace]</stacktrace>
<classname>[The exception class name]</classname>
<requestURI>[The URI of the original request]</requestURI>
</exception>

An example JSON exception return is:

{"results":{"errorcode":"[error code]",
"stacktrace":"[The exception stack trace]",
"classname":"[The exception class name]",
"requestURI":"[The URI of the original request]"}}