SpringWS(一)-环境搭建

    最近在用SpringWS,在学习的过程遇到不少问题,毕竟现在比较普遍的资料基本是SpringWS的官方文档和《Spring Web Services 2 Cookbook》,中文资料比较少,所以在此记录一下自己的学习笔记,如果有什么不对的地方,希望大家可以多多提点。

    官方帮助手册:http://docs.spring.io/spring-ws/site/reference/html/tutorial.html

    一、准备工作:

  1. 首先要清楚Spring Web Service是基于契约优先的,而不是代码优先,至于契约优先跟代码优先的优缺点,不在这里详细叙述,所以要通过Spring Web Service发布接口服务,首先要定义好xsd或者wsdl文件。
  2. 添加相关的依赖,在做Demo的过程中,我没有采用maven的方式构建项目,这里是相关jar包的下载地址(最新版本是出到2.1.4): http://ishare.iask.sina.com.cn/f/35615085.html 

    二、定义相关服务的Schema:

  1. 定义Schema,主要功能包括定义一个User类型,包括用户名和密码两个属性,xsd还定义对应的增加和查询操作。

     

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:userService="http://localhost/ws/UserService" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://localhost/ws/UserService" 
    elementFormDefault="unqualified" attributeFormDefault="unqualified">
        <xs:element name="AddRequest">
            <xs:complexType>
                <xs:all>
                    <xs:element ref="userService:User" maxOccurs="1" minOccurs="1"></xs:element>
                </xs:all>
            </xs:complexType>
        </xs:element>
        <xs:element name="AddResponse">
            <xs:complexType>
                <xs:all>
                    <xs:element name="success" type="xs:boolean"/>
                </xs:all>
            </xs:complexType>
        </xs:element>
        <xs:element name="GetUserByUserNameRequest">
            <xs:complexType>
                <xs:all>
                <xs:element name="username">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:maxLength value="20"></xs:maxLength>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                </xs:all>
            </xs:complexType>
        </xs:element>
        <xs:element name="GetUserByUserNameResponse">
            <xs:complexType>
                <xs:all>
                    <xs:element ref="userService:User" maxOccurs="1" minOccurs="1"></xs:element>
                </xs:all>
            </xs:complexType>
        </xs:element>
        <!--定义User类型-->
        <xs:element name="User">
            <xs:complexType>
                <xs:all>
                    <xs:element name="username">
                        <xs:simpleType>
                            <xs:restriction base="xs:string">
                                <xs:pattern value="[a-zA-Z0-9]{1,20}"/>
                            </xs:restriction>
                        </xs:simpleType>
                    </xs:element>
                    <xs:element name="password">
                        <xs:simpleType>
                            <xs:restriction base="xs:string">
                                <xs:minLength value="6"/>
                                <xs:maxLength value="15"/>
                            </xs:restriction>
                        </xs:simpleType>
                    </xs:element>
                </xs:all>
            </xs:complexType>
        </xs:element>
    </xs:schema>

     

PS:这里的定义有几个点要想说明一下:

1)如果我们要定义一个接口,里面有add(),就已经足够了,但是Spring-WS应该是约定俗成的,要我们在每个请求方法后面加“Request”,在返回的时候加上"Response",那AddRequest中名字为User的element就好比我们要传的参数(User类型),因为在<sws:dynamic-wsdl/>标签里面有一个属性(suffixRequest)来设置请求的后缀的。

2)还要注意你这个xsd定义的targetNamespace,就是你的命名空间,后面会有很多地方会引用到这个命名空间,最常见的错误就是“No adapter for endpoint”之类的,所以首先你要保证你的xsd合法,还要记住你自己定义的命名空间(http://localhost/ws/UserService)。

    三、搭建服务:

  1. SpringWS是依赖于SpringMVC的,所以要在web.xml进行相关的配置:(web.xml配置)
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        version="2.5">
        <display-name></display-name>
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
        <servlet>
            <servlet-name>spring-ws</servlet-name>
            <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
            <!--Spring-WS配置文件的位置,默认寻找[servlet-name]-servlet.xml文件 -->
            <!--<init-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/spring-ws-config.xml</param-value> 
                </init-param> -->
            <init-param>
                <param-name>transformWsdlLocations</param-name>
                <param-value>true</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>spring-ws</servlet-name>
            <url-pattern>/service/*</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>spring-ws</servlet-name>
            <url-pattern>*.wsdl</url-pattern>
        </servlet-mapping>
    </web-app>

     PS:transformWsdlLocations设置成true,就是说当你改变了项目名称或者端口号,你的服务还是可以正常访问的(但是如果你改变了WSDL的访问路径,发布服务的代码也要随之更改的)。

  2. 这里Spring-WS的配置文件我们使用spring-ws-servlet.xml(因为我们的servlet-name为spring-ws,所以对应的配置文件是spring-ws.servlet.xml,放在WEB-INF目录下):
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:sws="http://www.springframework.org/schema/web-services"
        xmlns:int="http://www.springframework.org/schema/integration" 
        xmlns:ws="http://www.springframework.org/schema/integration/ws"
        xmlns:util="http://www.springframework.org/schema/util" 
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/integration/ws
        http://www.springframework.org/schema/integration/ws/spring-integration-ws-2.2.xsd
        http://www.springframework.org/schema/web-services
        http://www.springframework.org/schema/web-services/web-services-2.0.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.1.xsd
        http://www.springframework.org/schema/integration
        http://www.springframework.org/schema/integration/spring-integration-2.2.xsd">
    <sws:dynamic-wsdl id="UserService" portTypeName="UserServicePortType" targetNamespace="http://localhost/ws/UserService" locationUri="/service" serviceName="UserService"> <sws:xsd location="/WEB-INF/xsd/UserService.xsd" /> </sws:dynamic-wsdl> </beans>

     PS:在开发阶段推荐使用动态生成wsdl文件,当系统发布之后,应该静态引入wsdl文件:

    <sws:static-wsdl id="orders" location="/WEB-INF/wsdl/orders.wsdl"/>

     

  3. 最后一步就是把我们之前定义好的UserService.xsd放到项目中,我是放在:WEB-INF/xsd/UserService.xsd
  4. 启动项目,地址栏输入:http://localhost:8080/Spring-WS-Demo-01/service/UserService.wsdl
  5. 如果缺少jar包,请自行解决。如果请求默认会被struts2过滤并获取,无法到达后端,可以自己重写StrutsPrepareAndExecuteFilter这个类,添加相关的过滤条件,不要让struts过滤掉请求。

    今天先写到这里,还有其他事情忙,有空会继续更新的,谢谢。有什么问题可以发邮件到:lizebin0918@163.com。

    另外,Demo的源码是放在:https://github.com/hippo0918/Spring-WS-Demo.git

 

posted @ 2014-04-15 21:44  lizebin0918  Views(3931)  Comments(0Edit  收藏  举报