此应用程序由以下主要实体组成:
<types>
<schema
targetNamespace="urn:SchemaDefinedPurchaseOrderService"
xmlns:soap11-enc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns="http://www.w3.org/2001/XMLSchema">
<xsd:import
namespace="http://java.sun.com/blueprints/ns/po"
schemaLocation="PurchaseOrder.xsd"/>
<element name="submitPO">
<complexType>
<sequence>
<element name="inputDoc" type="pons:PurchaseOrder"
nillable="true"/>
</sequence>
</complexType>
</element>
<element name="submitPOResponse">
<complexType>
<sequence>
<element name="result" type="string"
nillable="true"/>
</sequence>
</complexType>
</element>
<element name="InvalidPOException">
<complexType>
<sequence>
<element name="message"
type="string" nillable="true"/>
</sequence>
</complexType>
</element>
</schema>
</types>
<message
name="SchemaDefinedPurchaseOrderServiceSEI_submitPO">
<part name="parameters"
element="tns:submitPO"/>
</message>
<message
name="SchemaDefinedPurchaseOrderServiceSEI_submitPOResponse">
<part name="result"
element="tns:submitPOResponse"/>
</message>
<message name="InvalidPOException">
<part name="InvalidPOException"
element="tns:InvalidPOException"/>
</message>
<portType name="SchemaDefinedPurchaseOrderServiceSEI">
<operation name="submitPO">
<input
message="tns:SchemaDefinedPurchaseOrderServiceSEI_submitPO"/>
<output
message="tns:SchemaDefinedPurchaseOrderServiceSEI_submitPOResponse"/>
<fault name="InvalidPOException"
message="tns:InvalidPOException"/>
</operation>
</portType>
代码示例 1: 服务的 WSDL 中的代码片段<xsd:import namespace="http://java.sun.com/blueprints/ns/po" schemaLocation="PurchaseOrder.xsd"/>
导入到代码示例 1 中的 WSDL 文件的<?xml version="1.0"
encoding="UTF-8"?>
<xsd:schema targetNamespace="http://java.sun.com/blueprints/ns/po"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://java.sun.com/blueprints/ns/po"
elementFormDefault="qualified">
<xsd:element name="PurchaseOrder" type="PurchaseOrder"/>
<xsd:complexType name="Address">
<xsd:sequence>
<xsd:element name="street" type="xsd:string" nillable="false"/>
<xsd:element name="city" type="xsd:string" nillable="false"/>
<xsd:element name="state" type="xsd:string" nillable="false"/>
<xsd:element name="postalCode" type="xsd:string"
nillable="false"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="LineItem">
<xsd:sequence>
<xsd:element name="itemId" type="xsd:string" nillable="false"/>
<xsd:element name="price" type="xsd:float" nillable="false"/>
<xsd:element name="quantity" type="xsd:int" nillable="false"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="PurchaseOrder">
<xsd:sequence>
<xsd:element name="poId" type="xsd:string" nillable="false"/>
<xsd:element name="createDate" type="xsd:date" nillable="false"/>
<xsd:element name="shipTo" type="Address" nillable="false"/>
<xsd:element name="billTo" type="Address" nillable="false"/>
<xsd:element name="items" type="LineItem" nillable="false"
minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
代码示例 2: 订单结构
submitPO()
方法,该方法用于接收 PurchaseOrder
对象。此时会生成 PurchaseOrder
类,并与导入到 WSDL 文件中的订单结构相匹配。此外,还从 WSDL 生成了应用程序定义的异常 InvalidPOException
。public interface SchemaDefinedPurchaseOrderServiceSEI extends
Remote {
public String submitPO(PurchaseOrder inputDoc)
throws
InvalidPOException,
RemoteException;
}
getPoId()
方法仅将订单 ID 返回至客户端。在实际情况下,将结果返回至客户端之前还需要执行一些操作。此外,为了演示如何抛出特定于应用程序的异常,端点会检查订单 ID 是否为特定的编号,并在满足条件时抛出异常。端点实现类会如下所示:public class SchemaDefinedPurchaseOrderServiceBean implements
SessionBean {
private SessionContext sc;
public
SchemaDefinedPurchaseOrderServiceBean(){}
public String submitPO(PurchaseOrder po) throws
InvalidPOException, RemoteException
{
//this is done just to
illustrate throwing an application specific exception
if(po.getPoId().equals("100"))
throw new InvalidPOException("Invalid ID for the purchase order!!! " +
"For demo purposes, we throw " +
"an application defined exception for the ID value of
100.");
//extract the PO ID and
return to the client
return po.getPoId();
}
//life cycle methods
public void ejbCreate() throws CreateException
{}
public void setSessionContext(SessionContext sc) {
this.sc = sc;
}
public void ejbRemove(){}
public void ejbActivate() {}
public void ejbPassivate() {}
}
代码示例 4: 端点实现
<enterprise-beans>
<session>
<ejb-name>SchemaDefinedPurchaseOrderServiceBean</ejb-name>
<service-endpoint>com.sun.j2ee.blueprints.objectposervice.SchemaDefinedPurchaseOrderServiceSEI</service-endpoint>
<ejb-class>com.sun.j2ee.blueprints.objectposervice.SchemaDefinedPurchaseOrderServiceBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
代码示例 5:ejb-jar.xml 部署描述符中的代码片段 <service-endpoint>
。<webservice-description>
<webservice-description-name>SchemaDefinedPurchaseOrderService</webservice-description-name>
<wsdl-file>META-INF/wsdl/SchemaDefinedPurchaseOrderService.wsdl</wsdl-file>
<jaxrpc-mapping-file>META-INF/schemadefinedpurchaseorderservice-mapping.xml</jaxrpc-mapping-file>
<port-component>
<description>port component
description</description>
<port-component-name>SchemaDefinedPurchaseOrderService</port-component-name>
<wsdl-port
xmlns:PurchaseOrderns="urn:SchemaDefinedPurchaseOrderService">
PurchaseOrderns:SchemaDefinedPurchaseOrderServiceSEIPort
</wsdl-port>
<service-endpoint-interface>
com.sun.j2ee.blueprints.objectposervice.SchemaDefinedPurchaseOrderServiceSEI
</service-endpoint-interface>
<service-impl-bean>
<ejb-link>SchemaDefinedPurchaseOrderServiceBean</ejb-link>
</service-impl-bean>
</port-component>
</webservice-description>
代码示例 6:webservice.xml 部署描述符中的代码片段