Tutorials > Program model > Web services > Create an Atom feed to display recipes in a remote widget

< Previous | Next >


Run a pattern to create the Java classes for the recipe Atom feeds

In this lesson, you learn how to create two Java classes that generate Atom feeds for the four URLs you defined. One Java class will generate the two feeds from the Project noun, and the other Java class will generate the two feeds from the ProjectCollection noun. To generate the required Java class files, WebSphere Commerce provides the JET RESTful Resource pattern. You must provide XML input to the pattern, and the pattern generates the Java classes. This saves you from having to create the Java classes yourself.

Read the following topic so that you have the background knowledge required to understand this lesson:

The Java classes you create in this lesson are called JAX-RS resources. These Java classes convert RESTful HTTP GET requests into WebSphere Commerce service requests, and then return the data in Atom format. Each Java class uses the @Path annotation to indicate which URLs the class handles. You will create two Java classes, each containing a different @Path annotation:


Procedure

  1. Open WebSphere Commerce Developer and switch to the Enterprise Explorer view.

  2. Create the pattern input file for the Java class that will handle the URLs for Feed 1 and Feed 2 for the Project noun:

    1. Right-click the WebSphereCommerceServerExtensionsLogic project and select New > Folder.

    2. Type RestInterfaceDefinition as the folder name. This creates a single location to store all RESTful Resource pattern input files.

    3. Right-click the RestInterfaceDefinition folder and select New > File.

    4. Type the following name for the file: Project.xml

    5. Click Finish.

    6. Copy the following pattern input file XML snippet into the Project.xml file:

      <?xml version="1.0" encoding="utf-8"?>
      <rest componentName="Project" packageNamePrefix="com.mycompany.commerce">
          <noun name="Project" defaultAccessProfile="MyCompany_Store_Details" pluralNounName="Projects" 
              defaultExpression="/Project[search(ends-with(ProjectIdentifier/ExternalIdentifier/Name, ''))]">
              <findBy name="ProjectIdentifier" expression="/Project[ProjectIdentifier[(UniqueID={0})]]" />
          </noun>
      </rest>
      

      The following list describes the variables in the pattern input file. The component, noun, access profiles, and XPath expressions specified in this pattern input file were created in Tutorial: Creating the Project BOD service module.

      For the <rest> element:

      componentName

      Project is the name of the component that contains the Web service from which to create a feed.

      packageNamePrefix

      com.mycompany.commerce is the prefix you define for the package that will contain the Java class output by this pattern.

      For the <noun> element:

      name

      Project is the name of the noun that you want to get from the Web service for the Atom feed.

      defaultAccessProfile

      MyCompany_Store_Details is the name of the default storefront access profile to use for the WebSphere Commerce service request. The access profile defines the data to include in the response.

      pluralNounName

      Projects is the plural form of the noun. This value must match the pluralized name of the noun you included in the URLs for Feed 1 and Feed 2.

      defaultExpression

      /Project[search(ends-with(ProjectIdentifier/ExternalIdentifier/Name, ''))] is the XPath expression for the service that gets all recipes defined for the Project noun, rather than returning a specific recipe by its identifier. This XPath expression supports the URL for Feed 1.

      For the <findBy> element, which creates a method in the Java class to find a noun by its identifier using a specific XPath expression:

      name

      ProjectIdentifier is the suffix of the method name that is called when the URL contains an identifier. The pattern will create a method using this suffix for the name. In the generated Java class, the full method name will be findByProjectIdentifier.

      expression

      /Project[ProjectIdentifier[(UniqueID={0})]] is the XPath expression for the service that gets a noun using the identifier you specified in the URL. {0} is replaced with the identifier you specified in the URL. In this case the identifier is the UniqueID value of the recipe. This XPath expression supports the URL for Feed 2.

    7. Save the file.

  3. Create the pattern input file for the Java class that will handle the URLs for Feed 3 and Feed 4 for the ProjectCollection noun:

    1. Right-click the RestInterfaceDefinition folder and select New > File.

    2. Type the following name for the file: ProjectCollection.xml

    3. Click Finish.

    4. Copy the following pattern input file XML snippet into the ProjectCollection.xml file:

      <?xml version="1.0" encoding="utf-8"?>
      <rest componentName="Project" packageNamePrefix="com.mycompany.commerce">
          <noun name="ProjectCollection" defaultAccessProfile="MyCompany_Store_Details" pluralNounName="ProjectCollections" 
              defaultExpression="/ProjectCollection[search(ends-with(ProjectCollectionIdentifier/ExternalIdentifier/Name, ''))]">
              <findBy name="ProjectCollectionIdentifier" expression="/ProjectCollection[ProjectCollectionIdentifier[(UniqueID={0})]]" />
          </noun>
      </rest>
      

      Notice that this pattern input file contains data that is similar to the pattern input file for the Project noun, except that the data applies to the ProjectCollection noun instead.

    5. Save the file.

  4. Right-click the Project.xml pattern input file and select Run As > Input for Jet Transformation.

  5. In the Transformation section:

    1. Select the following ID, which is the JET RESTful Resource pattern:

      com.ibm.commerce.toolkit.internal.pattern.rest

    2. Click Run.
    The pattern generates the Java class for Feed 1 and Feed 2.

  6. Right-click the ProjectCollection.xml pattern input file and select Run As > Input for Jet Transformation.

  7. In the Transformation section:

    1. Select the following ID, which is the JET RESTful Resource pattern:

      com.ibm.commerce.toolkit.internal.pattern.rest

    2. Click Run.
    The pattern generates the Java class for Feed 3 and Feed 4.

  8. Right-click WebSphereCommerceServerExtensionsData, and then click Properties.

  9. Click Java EE Module Dependencies.

  10. In the JAR/Module column, check Project-Server.jar, Project-Client.jar, and Project-DataObjects.jar; then click OK.

  11. Find the generated Java class files, Project.java and ProjectCollection.java, at WebSphereCommerceServerExtensionsLogic > src > com.mycompany.commerce.project.rest.resources and ensure the files do not contain errors. For an explanation of the functions provided in sections of the generated Java classes, review the sample Java class in JET RESTful Resource pattern input file and the Java class it generates.

  12. Register the new Java classes to the JAX-RS runtime:

    1. Open the properties file at this path:

      WCDE_INSTALL/workspace/Rest/WebContent/WEB-INF/config/resources-ext.properties

    2. In the properties file, add the fully qualified names of the two Java classes the pattern created:

      com.mycompany.commerce.project.rest.resources.Project
      com.mycompany.commerce.project.rest.resources.ProjectCollection
      

    3. Save and close the file.

    4. Restart the WebSphere Commerce server.

In this lesson, you created the Java classes that support Atom feeds from the Get Project and Get ProjectCollection Web services. In the next lesson, we will create JSP files that format the Atom feeds containing recipe information.

< Previous | Next >


+

Search Tips   |   Advanced Search