Generating the configuration-based mapping files by using the REST mapping command

In this lesson, you use a utility to generate the XML mapping files for your sample data bean and controller command.

WebSphere Commerce uses mapping configuration files to make REST calls to beans and commands. These mapping files are divided into profiles, which use different types of calls for the same bean or command. For example, to specify different output values per profile.

Each configuration file defines the mappings for a single bean or command. The file describes mapping of REST input names to the corresponding bean or command setter methods. For each profile in the file, output mappings are also supplied to describe how the getter-methods from the bean or command must be mapped back to a REST entity output.

WebSphere Commerce provides a utility that is called RESTClassicSampleGen.bat. The utility creates initial mapping configuration files and sample REST resource handler code. The sample code can be used as a base to create your own REST service for your bean or command. The generated code demonstrates best practices, techniques, and conventions to assist with your customization. The utility helps you to generate a complete set of source code package elements.
Note: The generated files are samples that can be uses as a base. Do not expect to use the generated mappings without modifications.

For more information about the configuration-based command, bean mapping framework, and the utility command's usage, see Creating REST services using the configuration-based controller command and data bean mapping framework.

Procedure

  1. In a command prompt, browse to the WCDE_installdir\bin directory.
  2. Run the following commands:
    
    RESTClassicSampleGen.bat location=com.mycompany.commerce.mycomponent.beans.MyResourceListDataBean 
    outputDir=C:\RESTconfig additionalClassPath="C:\IBM\WCDE_INT80\workspace\WebSphereCommerceServerExtensionsLogic\bin"
     
    RESTClassicSampleGen.bat location=com.mycompany.commerce.mycomponent.beans.MyResourceDataBean 
    outputDir=C:\RESTconfig additionalClassPath="C:\IBM\WCDE_INT80\workspace\WebSphereCommerceServerExtensionsLogic\bin"
    
    RESTClassicSampleGen.bat location=com.mycompany.commerce.mycomponent.commands.MyResourceCreateCmd 
    outputDir=C:\RESTconfig additionalClassPath="C:\IBM\WCDE_INT80\workspace\WebSphereCommerceServerExtensionsLogic\bin"
    
    Where:
    location
    The fully qualified Java class name of the data bean or controller command, such as com.mycompany.commerce.mycomponent.beans.MyResourceDataBean.
    outputDir
    The output directory. If you do not have an existing one, the output directory is automatically created. Subdirectories are created for each class type generated.
    In this sample, the outputDir is C:\RESTconfig. If necessary, replace this value with your working_dir path.
    additionalClassPath
    additionalClassPath specifies extra JAR files and directories to locate classes (and their dependencies). Separate extra JAR files and directories with a semicolon.

    Ensure that the command is run successfully by checking the WCDE_installdir\logs\RESTclassic.log file.

    If you encounter issues by using the commands exactly as shown in the preceding snippet, remove the line breaks to ensure that you enter and submit each command as one line.

    The utility generates a set of directories and files for creating your REST service. It contains the following three folders under your outputDir folder:
    • controllerCommandSamples
    • dataBeanSamples
    • handlerSamples
    These folders represent the mapping configuration files and handler sample code for MyResourceDataBean, MyResourceListDataBean, and MyResourceCreateCmd.
  3. In the Enterprise Explorer view, go to REST > WebContent > WEB-INF > config.
  4. Create the beanMapping-ext and commandMapping-ext mapping folders.
    1. Right-click config. Click New > Folder.
    2. In the Folder name field, enter beanMapping-ext.
    3. Click Finish.
      The beanMapping-ext folder is created under the config directory.
    4. Right-click config. Click New > Folder.
    5. In the Folder name field, enter commandMapping-ext.
    6. Click Finish.
      The commandMapping-ext folder is created under the config directory.
  5. Copy the sample configuration to the REST configuration folder that you created in Step 4.
    1. Copy both com.mycompany.commerce.mycomponent.beans.MyResourceDataBean.xml and com.mycompany.commerce.mycomponent.beans.MyResourceListDataBean.xml from working_dir\dataBeanSamples to the new folder: beanMapping-ext.
    2. Copy com.mycompany.commerce.mycomponent.commands.MyResourceCreateCmd.xml from working_dir\controllerCommandSamples to the new folder commandMapping-ext.
    Your file structure resembles the following image:
    File structure
  6. Review the sample mapping configuration files:
    1. Go to REST > WebContent > WEB-INF > config > beanMapping-ext.
    2. Open the com.mycompany.commerce.mycomponent.beans.MyResourceDataBean.xml file.
      The following code displays:
      
      <?xml version="1.0" encoding="UTF-8"?>
      <bean>
           <profiles>
                <profile name="MyCompany_Databean_Summary">
                     <inputs>
                          <input inputName="myResourceId" methodName="setMyResourceId"/>
                     </inputs>
                     <outputs>
                          <output methodName="getField1" outputName="field1"/>
                          <output methodName="getField2" outputName="field2"/>
                          <output methodName="getMyResourceId" outputName="myResourceId"/>
                     </outputs>
                </profile>
           </profiles>
      </bean> 
      
      Note: The profile name in the file is MyCompany_Databean_Summary. When you run your own sample command from scratch, the profile name would otherwise be sample.
    3. Open the com.mycompany.commerce.mycomponent.beans.MyResourceListDataBean.xml file. Ensure that the profile name is MyCompany_DatabeanList_Summary.
    4. Go to REST > WebContent > WEB-INF > config > commandMapping-ext > .
    5. Open the com.mycompany.commerce.mycomponent.commands.MyResourceCreateCmd.xml file.
      The following code displays:
      
      <?xml version="1.0" encoding="UTF-8"?>
      <command>
           <profiles>
                <profile name="MyCommand_Summary">
                     <inputs>
                          <input inputName="field1" methodName="setField1"/>
                          <input inputName="field2" methodName="setField2"/>
                     </inputs>
                     <outputs>
                          <output methodName="getMyResourceId" outputName="myResourceId"/>
                     </outputs>
                </profile>
           </profiles>
      </command>
      
      Note: The profile name in the file is MyCommand_Summary. When you run your own sample command from scratch, the profile name would otherwise be sample.

    The mapping configuration files for your custom data bean and controller command are created.

    Next, you must create your own REST resource handler.