Java for Web学习笔记(八四):SOAP(1)小例子准备

来源:互联网 发布:韩子高 网络剧 编辑:程序博客网 时间:2024/06/05 04:00

SOAP现在碰到不多,一般是较为旧的系统。SOAP可以封装在不同的消息,在一个web app中,使用最为方便的自然是封装在HTTP的消息中。我们将在小例子中增加第三个上下文,用于实现soap api。

Schema

SOAP是contract-first的开发,我们先定义其schema。soap的body是xml,通过wsdl语言来定义,这是soap的关键核心。文件为*.xsd。在Eclipse中可以通过图形工具来帮助我们编写。

下面是利用工具生成的support.xsd。

<?xml version="1.0" encoding="UTF-8"?><schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/support" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns:support="http://www.example.org/support">    <element name="ticketsRequest" type="support:ticketsRequestType"></element>    <complexType name="ticketsRequestType"></complexType>   ... ...</schema>
看起来有些乱,接口定义和数据定义混在一起,看看书中给的例子,将namespace稍微修改,改成和自动生成的一致,另外增加了一些注释。
<?xml version="1.0" encoding="UTF-8"?><xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"           targetNamespace="http://www.example.org/support"           xmlns:xs="http://www.w3.org/2001/XMLSchema"           xmlns:support="http://www.example.org/support">    <!-- 定义接口部分 -->    <xs:element name="ticketsRequest" type="support:ticketsRequestType" />    <xs:element name="ticketRequest" type="support:selectTicketType" />    <xs:element name="createTicket" type="support:createTicketType"/>    <xs:element name="deleteTicket" type="support:selectTicketType"/>    <xs:element name="ticket" type="support:ticketType" />    <xs:element name="tickets" type="support:ticketsType" />    <!-- 定义数据格式部分 -->    <xs:complexType name="ticketType">        <xs:sequence>            <xs:element type="xs:long" name="id" minOccurs="0"/>            <xs:element type="xs:string" name="customerName" minOccurs="0"/>            <xs:element type="xs:dateTime" name="dateCreated" minOccurs="0"/>            <xs:element type="xs:string" name="subject"/>            <xs:element type="xs:string" name="body"/>            <xs:element type="support:attachmentType" name="attachment"                        minOccurs="0" maxOccurs="unbounded"/>        </xs:sequence>    </xs:complexType>    <xs:complexType name="ticketsRequestType">        <xs:sequence />    </xs:complexType>    <xs:complexType name="selectTicketType">        <xs:sequence>            <xs:element type="xs:long" name="id"/>        </xs:sequence>    </xs:complexType>    <xs:complexType name="createTicketType">        <xs:sequence>            <xs:element type="support:ticketType" name="ticket"/>        </xs:sequence>    </xs:complexType>    <xs:complexType name="ticketsType">        <xs:sequence>            <xs:element type="support:ticketType" name="ticket" minOccurs="0"                        maxOccurs="unbounded"/>        </xs:sequence>    </xs:complexType>    <xs:complexType name="attachmentType">        <xs:sequence>            <xs:element type="xs:string" name="name"/>            <xs:element type="xs:string" name="mimeContentType"/>            <xs:element type="xs:base64Binary" name="contents"/>        </xs:sequence>    </xs:complexType></xs:schema>

新增依赖: Spring Web Services

Spring Web Services项目独立于spring framework,实现对soap支持,另外,我们需要增加wsdl(Web Services Description Language)的支持,否则会报:
Caused by: java.lang.NoClassDefFoundError: javax/wsdl/extensions/ExtensibilityElement
在pom.xml文件中添加:
 <dependency>   <groupId>org.springframework.ws</groupId>   <artifactId>spring-ws-core</artifactId>   <version>2.4.0.RELEASE</version> </dependency> <dependency>   <groupId>wsdl4j</groupId>   <artifactId>wsdl4j</artifactId>   <version>1.6.3</version> </dependency>

相关链接: 我的Professional Java for Web Applications相关文章

阅读全文
0 0
原创粉丝点击