# XML Strategies and Web Service Design

WebLogic Workshop provides several strategies for handling XML messages in web services. Which strategy you should use depends on how the web services you are designing fit into the overall application.

## Strategy Criteria

The strategy you should use when implementing a web service depends on two criteria:

1. Is there existing application logic you are exposing? Specifically, are there Java classes you are using internally that are suitable to be exposed as data types in the web service's interface?
2. Will you define the interface to the web service, or is the interface externally defined? For example, is there a document schema your web service must accept?

The following diagram illustrates the matrix of strategies you may use depending on how your situation fits the preceding criteria:

### Strategy A: Expose Existing Classes

In this scenario, you have existing business logic and data types you would like to expose as one or more web services, and there are no external restrictions on the interface you expose. Specifically, there are no pre-defined external data types or schema that you must support.

If the existing Java classes are suitable to be exposed in the public interface, the easiest approach in this situation is to use the classes directly as parameters and return values of the methods of your web service(s). WebLogic Workshop will automatically use default XML maps to express the data types as XML schema in the web service's WSDL file.

If the internal classes are not suitable to expose as public data types, define new types to be exposed in the public interface and then map the public types to the internal types using XQuery Maps by following the steps in Strategy C below.

### Strategy B: Expose XML Schema

In this scenario, there are no existing data structures and no external requirements. The easiest approach is to create XML schema describing the data types you would like to use in the public interface. Place these schema in the Schemas project in your application and use the resulting XMLBeans types for the parameters of your web service's methods. WebLogic Workshop will use your schemas for the data types in the web service's WSDL file.

### Strategy C: Use XQuery Maps

In this scenario, the message formats for your web service's methods are specified externally but you have existing internal data types that you wish to use. To accomplish this, you need to use XQuery Maps to map the data in the incoming messages to fields in your internal data types. You can do this by following these steps:

1. Define your web service's methods using the internal Java types as parameters
2. Create an XML schema describing the incoming message format for each method (or use existing schemas if appropriate)
3. Add the schema(s) to the Schemas project in your application
4. For a method on the web service, in Design View, double-click on the method arrow to bring up the XQuery Mapper (or select the "..." button on the parameter-xml property's schema-element attribute in the Property Editor).
5. Select the Choose button next to the XML Message Schema text box.
6. Choose a schema type to use as the incoming message format for the method.
7. Use the XQuery Mapper to designate how to map data from the incoming messages to the Java types used as method parameters.

Note: When you specify a schema-element as the incoming message format for a web service method, you are defining the complete message format for that method. If you want to share a single data type across multiple methods you must define a different outermost element for each method, each of which contains the shared data type. WebLogic Workshop maps incoming XML messages to web service operations based on the complete message schema, so message to method relationships must be one-to-one.

To learn more about XQuery Maps, see Introduction to XQuery Maps.

### Strategy D: Implement a Pre-defined WSDL

In this scenario, the data types in your web service's public interface are externally specified (in a WSDL file) and you have no existing internal types. In this case, you may use the data types defined in the WSDL directly, as follows:

1. Copy the WSDL file to the Schemas project in your application. The schemas defined in the WSDL file will be compiled into XMLBeans.
2. Use the XMLBeans types directly as the arguments to your web service's methods.

Related Topics

None.