Example of creating a web service to search for an item
This example shows how to complete a search in the Java™ API and illustrates how to return an array of items rather than a single value.
About this task
Procedure
- Write the Java code.
The Java API offers a JDBC-like
search capability, which provides an equivalent to the JDBC statement
and result set that most Java programmers
who use JDBC are familiar with. The following sample Java code demonstrates the use of the Java API to create a Java API search and process the results.
public class SearchService { /** * Search a catalog for a specific spec's attribute. * * @param catalog * the catalog to search * @param spec * the spec to search * @param attribute * the attribute to search for * @return the result set as an array of Strings */ public String[] search(String catalog, String spec, String attribute) { Context context = null; SearchResultSet searchResultSet = null; String[] searchResults = null; try { // Obtain a PIM Context and a Search Manager context = PIMWebServicesContextFactory.getContext(); } catch (Exception e1) { e1.printStackTrace(); } // Build a search string and obtain a Search query instance String queryString = "select item ['" + spec + "/" + attribute + "'] from catalog('" + catalog + "')"; System.out.println("Query string built as : " + queryString); SearchQuery query; try { query = context.createSearchQuery(queryString); // Execute the query searchResultSet = query.execute(); // Process the Search result Set if (searchResultSet != null && searchResultSet.size() > 0) { String currentResult = null; searchResults = new String[searchResultSet.size()]; int resultsIndex = 0; while (searchResultSet.next()) { currentResult = searchResultSet.getString(1); System.out.println("Result : " + resultsIndex + " is : " + currentResult); // Add to the result set searchResults[resultsIndex] = currentResult; resultsIndex++; } } else { // No results so return empty array searchResults = new String[] {}; } } catch (PIMInternalException pie) { pie.printStackTrace(); } catch (PIMAuthorizationException pae) { pae.printStackTrace(); } catch (PIMSearchException pse) { pse.printStackTrace(); } return searchResults; } }
- Deploy the user .jar file.
For more information, see Deploying a third party or custom user .jar file.
- Register the web service in IBM® Product Master.
- Access your Product Master instance
and log in. For example:
http://yourWPCserver:yourWPCport/utils/enterLogin.jsp
. - Click Collaboration Manager > Web Services > Web Service Console > New.
- Provide the following values:
- Web Service Name: Provide a name. For example, SearchService.
- WSDL: Type
<definition/>
to indicate that Product Master should generate the WSDL for you. - Web Service Implementation: Select Java.
- Java Implementation Class: Type the Java class of your web service.
In the above example, you would type
com.acme.javawbs.SearchService
. - Deployed: Select this check box. Clearing this selection enables you to store web services in an inactive (unusable) state.
- Authenticated: Select this check box to have the user name, company, and password that is specified when the web service is started.
- Click Save. If you get an error
similar to:
Unable to verify Java implementation class
, and you have no typographical errors in your fully qualified Java class name, then you did not successfully deploy your Java class through the user .jar mechanism. Return to Step 2 and check whether your user .jar appears in your class path.For example,ps -ef | grep java
and check the Java process for Product Master.
- Access your Product Master instance
and log in.
- Test starting your web service. After you retrieve a query,
you can now use the
execute()
method to run the query and process the returned search result set:Where YourWPCServer and YourWPCPort is the URL for accessing your Product Master system, ServiceName is the name that you gave the web service when you deployed it to Product Master, and YourSpecNameYourAttributeName, and YourCatalogName are the spec, attribute, and catalog that you want to query.http://YourWPCServer:YourWPCPort /services/ServiceName?method=search&spec=YourSpecName&attribute=YourAttributeName&catalogName=YourCatalogName
Because you selected Authenticated, an Axis login dialog opens. Enter the appropriate user name and password, for example: User name:
Joe@Acme
Password:passw0rd
. The output in the browser window returns three items from the catalog.