Spring WebService是契约优先的。强调从Schema(xml)到Java Class的绑定。所以首先要熟悉XSD和JiBX或JAXB等binding技术,为了方便,还需属性ant脚本。
闲话少说,贴一个成功部署的war代码。
Java: springstudy.ws.
client.SpringWSClient.java
core.BeanMethodDescriptor.java
|--.WebMethod.java
|---WebService.java
|---WebMethodPostProcessor.java
|---WebServiceErrorResponse.java
endpoint.SpringWebServiceEndpoint.java
services.CustomerService.java
util.BindXmlUtil.java
-----------------------------------------------------------------------
/WEB-INF/schema/test-request.xsd
<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="http://www.99bill.com/schema/test" xmlns:test="http://www.99bill.com/schema/test" xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<element name="test-request" type="test:TestRequest" />
<complexType name="TestRequest">
<sequence>
<element name="request-header" type="test:RequestHeader" />
<element name="request-body" type="test:RequestBody" />
</sequence>
</complexType>
<complexType name="RequestHeader">
<sequence>
<element name="service" type="string" />
<element name="version" type="string" />
<element name="method" type="string" />
</sequence>
</complexType>
<complexType name="RequestBody">
<sequence>
<element name="customer-no" type="int" />
<element name="first-name" type="string" />
<element name="last-name" type="string" />
<element name="phone" type="string" />
</sequence>
</complexType>
</schema>
-----------------------------------test-response.xsd
<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="http://www.99bill.com/schema/test" xmlns:test="http://www.99bill.com/schema/test" xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<element name="test-response" type="test:TestResponse" />
<complexType name="TestResponse">
<sequence>
<element name="customer" type="test:Customer"/>
</sequence>
</complexType>
<complexType name="Customer">
<sequence>
<element name="person" type="test:Person"/>
<element name="street" type="string" />
<element name="city" type="string" />
<element name="state" type="string" />
<element name="zip" type="integer" />
<element name="phone" type="string" />
</sequence>
</complexType>
<complexType name="Person">
<sequence>
<element name="customer-no" type="int" />
<element name="first-name" type="string" />
<element name="last-name" type="string" />
</sequence>
</complexType>
</schema>
---------------------------------------------------------------------
WEB-INF/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" id="WebApp_ID" version="2.5">
<display-name>test-springws</display-name>
<servlet>
<servlet-name>spring-ws</servlet-name>
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
<init-param>
<param-name>transformWsdlLocations</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring-ws</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
--------------------------------------------------------------
WEB-INF/spring-ws-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xmlns:sws="http://www.springframework.org/schema/web-services"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-1.5.xsd
http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-1.5.xsd">
<bean id="customerXsd" class="org.springframework.xml.xsd.commons.CommonsXsdSchemaCollection">
<property name="xsds" >
<list>
<value>/WEB-INF/schema/test-request.xsd</value>
<value>/WEB-INF/schema/test-response.xsd</value>
</list>
</property>
<property name="inline" value="false"/>
</bean>
<!-- ===================== WSDL DEFINITION ============================== -->
<bean id="customer" class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition">
<property name="schemaCollection" ref="customerXsd"/>
<property name="requestSuffix" value="-89request64"/>
<property name="responseSuffix" value="-89response64"/>
<property name="portTypeName" value="Customer"/>
<property name="locationUri" value="http://localhost/test-springws/services"/>
<property name="targetNamespace" value="http://www.99bill.com:88/schema/test"/> <!--注意端口与server.xml设置一致-->
</bean>
<bean id="customerWebService"
class="com.tsinghuatec.dawn.waf.springstudy.ws.services.CustomerWebService">
<!-- property name="defaultUri" value="http://localhost:8080/test-springws/services" / -->
</bean>
<!--申明我们用注解来实现请求和Endpoint的映射对应
<bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping"/>
-->
<bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
<property name="defaultEndpoint" ref="defaultEndpoint" />
<property name="interceptors">
<list>
<bean
class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor" />
</list>
</property>
</bean>
<!-- extends from org.springframework.ws.server.endpoint.AbstractDom4jPayloadEndpoint -->
<bean id="defaultEndpoint" class="com.tsinghuatec.dawn.waf.springstudy.ws.endpoint.SpringWebServiceEndPoint">
<property name="oxm" value="jibx"/>
</bean>
<bean class="com.tsinghuatec.dawn.waf.springstudy.ws.core.WebMethodPostProcessor" />
</beans>