Best practices for mapping REST API methods to data beans
Follow the sample code and coding tips to ensure that your new or custom REST data bean services conform to WebSphere Commerce best practices.
Procedure
- When creating and naming data bean services:
- Create a resource handler for each data bean that uses the same noun by extending the AbstractClassicResourceHandler class. The recommended naming convention for the resource handler is DataBeanObjectDataBeanHandler. For example, CatalogDataBeanHandler, OrganizationDataBeanHandler.
- The URL naming convention for the resource handler is store/{storeId}/{DataBeanObject}. For example, store/11051/catalog.
- The data bean parameters (name-value pairs) are expected to be sent in the content by using a POST request.
- The response properties (
TypedProperty class
) are set by the resource handler, serialized, and returned. - An
accessProfile
query parameter should be used when you are filtering the amount of return data.
- The following sample code invokes a non-protected and a
protected data bean in the data bean resource handler:
// CatalogDataBean is a non-protected data bean // when there is no need to add/update/delete custom request properties, setting // to null will cause the code to read them from the content TypedProperty requestProperties = null; CatalogDataBean catalogDB = new CatalogDataBean(); response = activateDataBeanWithContext(accessProfile, catalogDB, requestProperties, responseFormat); // OrganizationDataBean is a protected databean // when there is no need to add/update/delete custom request properties, setting // to null will cause the code to read them from the content TypedProperty requestProperties = null; OrganizationDataBean organizationDB = new OrganizationDataBean(); response = activateDataBeanWithContext(accessProfile, organizationDB, requestProperties, responseFormat); /** * This methods sets up the BCS by calling startRequest(), * activates the databean, and then ends the BCS by calling * endRequest(BusinessContextService). * * @param accessProfile the access profile * @param dataBean the databean to be activated * @param requestProperties the request properties * @param responseFormat the response format * @return the response */ protected Response activateDataBeanWithContext(String, DataBean, TypedProperty, String);
- The following sample code generates the response properties
returned by the data bean. Override the following method in the data
bean resource handler:
protected TypedProperty composeDataBeanResponse(TypedProperty requestProperties, String accessProfile, DataBean dataBean) throws Exception { // default implementation: return empty responseProperties TypedProperty responseProperties = super.composeDataBeanResponse(requestProperties, accessProfile, dataBean); OrganizationDataBean organizationDB = (OrganizationDataBean)dataBean; responseProperties.put("displayName", organizationDB.getDisplayName()); responseProperties.put("distinguishedName", organizationDB.getDistinguishedName()); return responseProperties; } /** * This method constructs the data bean response. * * @param requestProperties the request properties * @param accessProfile the access profile * @param dataBean the activated data bean * @return the response properties * @throws Exception the exception occurs */ protected TypedProperty composeDataBeanResponse(TypedProperty, String, DataBean) throws Exception;
- The following sample code activates multiple data beans
in a custom resource handler:
// sample custom code to manipulate the request properties TypedProperty requestProperties = initializeRequestPropertiesFromRequestMap(responseFormat); requestProperties.put("storeId", storeId); StoreDataBean storeDB = new StoreDataBean(); activateDataBean(storeDB, requestProperties, responseFormat); TypedProperty responseProperties = new TypedProperty(); // custom code to examine activated data bean and add properties to the // response properties based on accessProfile parameter addToResponseAndRequest(responseProperties, requestProperties, accessProfile, storeDB); // might have custom code to manipulate the request properties CatalogDataBean catalogDB = new CatalogDataBean(); activateDataBean(catalogDB, requestProperties, responseFormat); // custom code to examine activated data bean and add properties to the // response properties based on accessProfile parameter addToResponseAndRequest(responseProperties, requestProperties, accessProfile, catalogDB); response = generateResponseFromHttpStatusCodeAndRespData(responseFormat, responseProperties.getMap(), HttpStatus.OK); endRequest(bcs); /** * This method creates a command context and associates the request properties to the command * context created. It then passes both the data bean and the command context to the data bean * manager for data bean activation. * * @param dataBean the data bean to be activated * @param requestProperties the request properties * @param responseFormat the responseFormat * @throws Exception the exception occurs */ protected void activateDataBean(DataBean, TypedProperty, String) throws Exception;
- Each REST resource contains information such as URLs, descriptions, and sample input and output data in the WebSphere Commerce REST API: