Métodos recomendados para correlacionar métodos de API REST con beans de datos
Siga el ejemplo de código y las sugerencias para la codificación para asegurarse de que los servicios de bean de datos de REST nuevos o personalizados se ajustan a los métodos recomendados de HCL Commerce.
Procedimiento
-
El código de ejemplo siguiente invoca un bean de datos no protegido y uno protegido en el manejador de recursos de bean de datos:
// 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);
-
El código de ejemplo siguiente genera las propiedades de respuesta devueltas por el bean de datos. Altere el método siguiente en el manejador de recursos de bean de datos:
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;
-
El código de ejemplo siguiente activa varios beans de datos en un manejador de recursos personalizado:
// 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;
- Cada recurso REST contiene información como URL, descripciones y datos de entrada y salida de ejemplo en la API REST de HCL Commerce: