
In this lesson, you learn
how to create your own REST resource handler for your sample data
bean and controller command. A REST call requires the creation of
a resource handler. The resource handler represents the entry point
for resource requests and is annotated with the @Path, context, and
other information that is required to handle a request. Handlers are
responsible for coordinating the client request. Handlers intercept
calls, run required actions, and convert responses to a form consumable
by the client by using standard HTTP protocol elements.
With the configuration-based command and bean mapping
framework, a REST call is made to access a data bean (GET) or a controller
command (POST). The handler is relatively small, and required to define
the annotations for Apache Wink to understand how to make the REST
call to the handler and supply the requested information.
Before you begin
Browse to the folder: working_dir\handlerSamples.
Review the sample handler code that is generated by the utility for
your data beans and controller command.By reviewing the sample
code, you learn that the custom resource handler extends from AbstractConfigBasedClassicHandler.
The actual code-content of the handler usually is one line of code,
consisting of the entry point into the configuration-based classic
handler. The sample REST handler accepts two resources, one that gets
the resource data from the MyResourceDataBean/MyResourceListDataBean(GET),
and the other that creates a data from a command (POST). You must
create the custom resource handler under WebSphereCommerceServerExtensionsLogic\src.
You can use one Java class to contain all the methods for your sample
data bean and controller command.
Procedure
- Start
WebSphere Commerce Developer.
- In the Enterprise Explorer view, browse to . Click .
- In the Name field, enter com.mycompany.commerce.rest.mycomponent.handler.
Click Finish.
- Right-click the com.mycompany.commerce.rest.mycomponent.handler package.
Click .
- Click Next. Click Browse and
browse to your sample code directory.
For example, C:\source\workspace\WebSphereCommerceServerExtensionsLogic\src\com\mycompany\commerce\rest\mycomponent\handler.
Select the Myresourcehandler.java file. Click Finish to
import the file. (The class file is built automatically, so no need
to copy it).
- Open the Myresourcehandler.java file.
The following methods are implemented:
- findByMyResourceId
- The REST handler runs the MyResourceDataBean based on the REST
input and the corresponding configuration mapping profile MyCompany_Databean_Summary.
This method is used to get the resource data from MyResourceDataBean
according to the myResourceId you specified. It uses findByMyResourceId
as method name, storeId, myResourceId,
and responseFormat as parameters, and store/{storeId}/my_resource/{myResourceId} as
the path.
- findMyResourceList
- The REST handler runs the MyResourceListDataBean based on the
REST input and the corresponding configuration mapping profile MyCompany_DatabeanList_Summary.
This method is used to get the list of resource data, such as, 0 to
more resource data from MyResourceListDataBean. It uses findMyResourceList
as method name, storeId, and responseFormat as
parameters, and the path that is defined for this method is store/{storeId}/my_resource/myResourceListDataBean.
- create
- The REST handler runs the MyResourceCreateCmd based on the REST
input and the corresponding configuration mapping profile MyCommand_Summary.
This method is used to create a new data by a command (POST). It uses
create as method name, storeId, and responseFormat as
parameters, and store/{storeId}/my_resource as
path. It retrieves context parameters from the query and other input
parameters from the request body.
You also can use GET
methods to implement queries, specifically find by primary key, find
by identifier/name, and find by query. Implement queries by using
find by query with internal GET methods. Avoid using GET methods with
custom paths as they must be manually created, since they are not
generated by the utility.
- initializeQueryRegistry
- This method initializes the new Query registry.
- FindByQuery
- This method is to implement the GET method. It uses findByQuery
as method name, storeId as parameter, and store/{storeId}/resource_nameas
path, such as /{storeId}/my_resource. It uses
the query registry framework to map query names to internal GET methods
per the REST API Discoverability.
- findSelf
- The REST handler runs the MyResourceListDataBean based on the
REST input and the corresponding configuration mapping profile MyCompany_DatabeanList_Summary.
This method is used to get the list of resource data created by the
current user. It uses findSelf as method name, storeId as
parameter, self as query name, the store/{storeId}/ my_resource?q=self as
path.
For more information about these methods, see the
local Swagger UI at the following URL:
http://localhost/webapp/wcs/stores/servlet/swagger/index.html.
- Register your new resource handler:
- In the Enterprise Explorer view, browse to the file.
- Define the new resource handler that you created:
# Extended Resources
com.mycompany.commerce.rest.mycomponent.handler.MyResourceHandler
- Save and close the file.
Next, verify your REST service calls in a web browser.