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.
Before you begin
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:
@Path("stores/{storeId}/Projects")
The class
with this annotation handles all URL requests matching .../stores/storeId/Projects/...
. Therefore
this class will handle the Feed 1 and Feed 2 URLs.
@Path("stores/{storeId}/ProjectCollections")
The
class with this annotation handles all URL requests matching .../stores/storeId/ProjectCollections/...
. Therefore
this class will handle the Feed 3 and Feed 4 URLs.
Procedure
- Open
WebSphere Commerce Developer and switch to the Enterprise Explorer
view.
- Create the pattern input file for the Java class that will
handle the URLs for Feed 1 and Feed 2 for the Project noun:
- Right-click the WebSphereCommerceServerExtensionsLogic
project and select .
- Type RestInterfaceDefinition as
the folder name. This creates a single location to store all RESTful
Resource pattern input files.
- Right-click the RestInterfaceDefinition folder
and select .
- Type the following name for the file: Project.xml
- Click Finish.
- 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" projectName="WebSphereCommerceServerExtensionsLogic" >
<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 a BOD service module.
For
the <rest>
element:
- componentName
Project
is the name of the component that contains
the Web service from which you want 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.
- projectName
- WebSphereCommerceServerExtensionsLogic is
the project under which you are creating the new package. This expression
is only required in FEP4.
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.
- Save the file.
- Create the pattern input file for the Java class that will
handle the URLs for Feed 3 and Feed 4 for the ProjectCollection noun:
- Right-click the RestInterfaceDefinition folder
and select .
- Type the following name for the file: ProjectCollection.xml
- Click Finish.
- 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" projectName="WebSphereCommerceServerExtensionsLogic" >
<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.
- Save the file.
- Right-click the Project.xml pattern
input file and select .
- In the Transformation section:
- Select the following ID, which is the JET RESTful Resource pattern:
com.ibm.commerce.toolkit.internal.pattern.rest
- Click Run.
The pattern generates the Java class for Feed 1 and Feed 2.
- Right-click the ProjectCollection.xml pattern
input file and select .
- In the Transformation section:
- Select the following ID, which is the JET RESTful Resource pattern:
com.ibm.commerce.toolkit.internal.pattern.rest
- Click Run.
The pattern generates the Java class for Feed 3 and Feed 4.
- Right-click WebSphereCommerceServerExtensionsData,
and then click Properties.
- Click Java EE Module Dependencies.
- In the JAR/Module column, check Project-Server.jar, Project-Client.jar,
and Project-DataObjects.jar; then click OK.
- Find the generated Java class files, Project.java and
ProjectCollection.java
,
at and ensure the files do not contain errors. - Register the new Java classes to the JAX-RS runtime:
- Open the properties file at this path:
WCDE_installdir/workspace/Rest/WebContent/WEB-INF/config/resources-ext.properties
- 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
- Save and close the file.
- Restart the WebSphere Commerce server.
Results
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, you will create JSP files that format the Atom
feeds containing recipe information.