Spring Web Service自带Demo浅析(二)

来源:互联网 发布:知乎怎么发文章 编辑:程序博客网 时间:2024/05/18 01:22

接上例:http://blog.csdn.net/kunshan_shenbin/archive/2009/01/07/3726803.aspx

这里我们对Spring Web Service技术做一下强化,例子仍然来自他自带的Demo。

如下图所示建立工程:

代码如下:

HolidayClient.java

  1. package com.mycompany.hr.client.sws;
  2. import java.io.IOException;
  3. import javax.xml.transform.Source;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. import org.springframework.core.io.Resource;
  7. import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
  8. import org.springframework.xml.transform.ResourceSource;
  9. public class HolidayClient extends WebServiceGatewaySupport {
  10.     private Resource request;
  11.     public void setRequest(Resource request) {
  12.         this.request = request;
  13.     }
  14.     public void holiday() throws IOException {
  15.         
  16.         Source requestSource = new ResourceSource(request);
  17.         getWebServiceTemplate().sendSourceAndReceiveToResult(requestSource, null);
  18.     }
  19.     public static void main(String[] args) throws IOException {
  20.         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml", HolidayClient.class);
  21.         HolidayClient holidayClient = (HolidayClient) applicationContext.getBean("holidayClient");
  22.         holidayClient.holiday();
  23.     }
  24. }

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
  5.     
  6.     <bean id="holidayClient" class="com.mycompany.hr.client.sws.HolidayClient">
  7.         <property name="defaultUri" value="http://localhost:8080/holidayService/services" />
  8.         <property name="request" value="classpath:com/mycompany/hr/client/sws/holidayRequest.xml" />
  9.     </bean>
  10.     
  11. </beans>

holidayRequest.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <HolidayRequest xmlns="http://mycompany.com/hr/schemas">
  3.     <Holiday>
  4.         <StartDate>2008-12-12</StartDate>
  5.         <EndDate>2009-01-01</EndDate>
  6.     </Holiday>
  7.     <Employee>
  8.         <Number>1</Number>
  9.         <FirstName>Shen</FirstName>
  10.         <LastName>Bin</LastName>
  11.     </Employee>
  12. </HolidayRequest>

HumanResourceService.java

  1. package com.mycompany.hr.service;
  2. import java.util.Date;
  3. public interface HumanResourceService {
  4.     void bookHoliday(Date startDate, Date endDate, String name);
  5. }

StubHumanResourceService.java

  1. package com.mycompany.hr.service;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import org.apache.commons.logging.Log;
  5. import org.apache.commons.logging.LogFactory;
  6. public class StubHumanResourceService implements HumanResourceService {
  7.     private static final Log logger = LogFactory.getLog(StubHumanResourceService.class);
  8.     public void bookHoliday(Date startDate, Date endDate, String name) {
  9.         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  10.         String str_sd = dateFormat.format(startDate);
  11.         String str_ed = dateFormat.format(endDate);
  12.         logger.info("Booking holiday for [" + str_sd + " ~ " + str_ed + "] for [" + name + "] ");
  13.     }
  14. }

HolidayEndpoint.java

  1. package com.mycompany.hr.ws;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import com.mycompany.hr.service.HumanResourceService;
  5. import org.jdom.Element;
  6. import org.jdom.JDOMException;
  7. import org.jdom.Namespace;
  8. import org.jdom.xpath.XPath;
  9. import org.springframework.ws.server.endpoint.AbstractJDomPayloadEndpoint;
  10. public class HolidayEndpoint extends AbstractJDomPayloadEndpoint {
  11.     private XPath startDateExpression;
  12.     private XPath endDateExpression;
  13.     private XPath nameExpression;
  14.     private HumanResourceService humanResourceService;
  15.     public HolidayEndpoint(HumanResourceService humanResourceService) throws JDOMException {
  16.         this.humanResourceService = humanResourceService;
  17.         Namespace namespace = Namespace.getNamespace("hr""http://mycompany.com/hr/schemas");
  18.         startDateExpression = XPath.newInstance("//hr:StartDate");
  19.         startDateExpression.addNamespace(namespace);
  20.         endDateExpression = XPath.newInstance("//hr:EndDate");
  21.         endDateExpression.addNamespace(namespace);
  22.         nameExpression = XPath.newInstance("concat(//hr:FirstName,' ',//hr:LastName)");
  23.         nameExpression.addNamespace(namespace);
  24.     }
  25.     protected Element invokeInternal(Element holidayRequest) throws Exception {
  26.         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  27.         Date startDate = dateFormat.parse(startDateExpression.valueOf(holidayRequest));
  28.         Date endDate = dateFormat.parse(endDateExpression.valueOf(holidayRequest));
  29.         String name = nameExpression.valueOf(holidayRequest);
  30.         humanResourceService.bookHoliday(startDate, endDate, name);
  31.         return null;
  32.     }
  33. }

hr.xsd

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3.   ~ Copyright 2007 the original author or authors.
  4.   ~
  5.   ~ Licensed under the Apache License, Version 2.0 (the "License");
  6.   ~ you may not use this file except in compliance with the License.
  7.   ~ You may obtain a copy of the License at
  8.   ~
  9.   ~      http://www.apache.org/licenses/LICENSE-2.0
  10.   ~
  11.   ~ Unless required by applicable law or agreed to in writing, software
  12.   ~ distributed under the License is distributed on an "AS IS" BASIS,
  13.   ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.   ~ See the License for the specific language governing permissions and
  15.   ~ limitations under the License.
  16.   -->
  17. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  18.            xmlns:hr="http://mycompany.com/hr/schemas"
  19.            elementFormDefault="qualified"
  20.            targetNamespace="http://mycompany.com/hr/schemas">
  21.     <xs:element name="HolidayRequest">
  22.         <xs:complexType>
  23.             <xs:all>
  24.                 <xs:element name="Holiday" type="hr:HolidayType"/>
  25.                 <xs:element name="Employee" type="hr:EmployeeType"/>
  26.             </xs:all>
  27.         </xs:complexType>
  28.     </xs:element>
  29.     <xs:complexType name="HolidayType">
  30.         <xs:sequence>
  31.             <xs:element name="StartDate" type="xs:date"/>
  32.             <xs:element name="EndDate" type="xs:date"/>
  33.         </xs:sequence>
  34.     </xs:complexType>
  35.     <xs:complexType name="EmployeeType">
  36.         <xs:sequence>
  37.             <xs:element name="Number" type="xs:integer"/>
  38.             <xs:element name="FirstName" type="xs:string"/>
  39.             <xs:element name="LastName" type="xs:string"/>
  40.         </xs:sequence>
  41.     </xs:complexType>
  42. </xs:schema>

spring-ws-servlet.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
  5.     <bean id="holidayEndpoint" class="com.mycompany.hr.ws.HolidayEndpoint">
  6.         <constructor-arg ref="hrService" />
  7.     </bean>
  8.     <bean id="hrService" class="com.mycompany.hr.service.StubHumanResourceService" />
  9.     <bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
  10.         <property name="mappings">
  11.             <props>
  12.                 <prop key="{http://mycompany.com/hr/schemas}HolidayRequest">
  13.                     holidayEndpoint
  14.                 </prop>
  15.             </props>
  16.         </property>
  17.         <property name="interceptors">
  18.             <bean
  19.                 class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor" />
  20.         </property>
  21.     </bean>
  22.     <bean id="holiday" class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition">
  23.         <property name="schema" ref="schema" />
  24.         <property name="portTypeName" value="HumanResource" />
  25.         <property name="locationUri" value="/holidayService/" />
  26.         <property name="targetNamespace" value="http://mycompany.com/hr/definitions" />
  27.     </bean>
  28.     <bean id="schema" class="org.springframework.xml.xsd.SimpleXsdSchema">
  29.         <property name="xsd" value="/WEB-INF/hr.xsd" />
  30.     </bean>
  31. </beans>

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  6.     
  7.     <display-name>MyCompany HR Holiday Service</display-name>
  8.     <servlet>
  9.         <servlet-name>spring-ws</servlet-name>
  10.         <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
  11.         <init-param>
  12.             <param-name>transformWsdlLocations</param-name>
  13.             <param-value>true</param-value>
  14.         </init-param>
  15.     </servlet>
  16.     <servlet-mapping>
  17.         <servlet-name>spring-ws</servlet-name>
  18.         <url-pattern>/*</url-pattern>
  19.     </servlet-mapping>
  20.     
  21. </web-app>

注意:

客户端holidayRequest.xml文件中, 

......

<HolidayRequest xmlns="http://mycompany.com/hr/schemas">

......

xmlns的值必须和服务端hr.xsd文件中targetNamespace的属性值一致。