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
- package com.mycompany.hr.client.sws;
- import java.io.IOException;
- import javax.xml.transform.Source;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.core.io.Resource;
- import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
- import org.springframework.xml.transform.ResourceSource;
- public class HolidayClient extends WebServiceGatewaySupport {
- private Resource request;
- public void setRequest(Resource request) {
- this.request = request;
- }
- public void holiday() throws IOException {
- Source requestSource = new ResourceSource(request);
- getWebServiceTemplate().sendSourceAndReceiveToResult(requestSource, null);
- }
- public static void main(String[] args) throws IOException {
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml", HolidayClient.class);
- HolidayClient holidayClient = (HolidayClient) applicationContext.getBean("holidayClient");
- holidayClient.holiday();
- }
- }
applicationContext.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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
- <bean id="holidayClient" class="com.mycompany.hr.client.sws.HolidayClient">
- <property name="defaultUri" value="http://localhost:8080/holidayService/services" />
- <property name="request" value="classpath:com/mycompany/hr/client/sws/holidayRequest.xml" />
- </bean>
- </beans>
holidayRequest.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <HolidayRequest xmlns="http://mycompany.com/hr/schemas">
- <Holiday>
- <StartDate>2008-12-12</StartDate>
- <EndDate>2009-01-01</EndDate>
- </Holiday>
- <Employee>
- <Number>1</Number>
- <FirstName>Shen</FirstName>
- <LastName>Bin</LastName>
- </Employee>
- </HolidayRequest>
HumanResourceService.java
- package com.mycompany.hr.service;
- import java.util.Date;
- public interface HumanResourceService {
- void bookHoliday(Date startDate, Date endDate, String name);
- }
StubHumanResourceService.java
- package com.mycompany.hr.service;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- public class StubHumanResourceService implements HumanResourceService {
- private static final Log logger = LogFactory.getLog(StubHumanResourceService.class);
- public void bookHoliday(Date startDate, Date endDate, String name) {
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
- String str_sd = dateFormat.format(startDate);
- String str_ed = dateFormat.format(endDate);
- logger.info("Booking holiday for [" + str_sd + " ~ " + str_ed + "] for [" + name + "] ");
- }
- }
HolidayEndpoint.java
- package com.mycompany.hr.ws;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import com.mycompany.hr.service.HumanResourceService;
- import org.jdom.Element;
- import org.jdom.JDOMException;
- import org.jdom.Namespace;
- import org.jdom.xpath.XPath;
- import org.springframework.ws.server.endpoint.AbstractJDomPayloadEndpoint;
- public class HolidayEndpoint extends AbstractJDomPayloadEndpoint {
- private XPath startDateExpression;
- private XPath endDateExpression;
- private XPath nameExpression;
- private HumanResourceService humanResourceService;
- public HolidayEndpoint(HumanResourceService humanResourceService) throws JDOMException {
- this.humanResourceService = humanResourceService;
- Namespace namespace = Namespace.getNamespace("hr", "http://mycompany.com/hr/schemas");
- startDateExpression = XPath.newInstance("//hr:StartDate");
- startDateExpression.addNamespace(namespace);
- endDateExpression = XPath.newInstance("//hr:EndDate");
- endDateExpression.addNamespace(namespace);
- nameExpression = XPath.newInstance("concat(//hr:FirstName,' ',//hr:LastName)");
- nameExpression.addNamespace(namespace);
- }
- protected Element invokeInternal(Element holidayRequest) throws Exception {
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
- Date startDate = dateFormat.parse(startDateExpression.valueOf(holidayRequest));
- Date endDate = dateFormat.parse(endDateExpression.valueOf(holidayRequest));
- String name = nameExpression.valueOf(holidayRequest);
- humanResourceService.bookHoliday(startDate, endDate, name);
- return null;
- }
- }
hr.xsd
- <?xml version="1.0" encoding="UTF-8"?>
- <!--
- ~ Copyright 2007 the original author or authors.
- ~
- ~ Licensed under the Apache License, Version 2.0 (the "License");
- ~ you may not use this file except in compliance with the License.
- ~ You may obtain a copy of the License at
- ~
- ~ http://www.apache.org/licenses/LICENSE-2.0
- ~
- ~ Unless required by applicable law or agreed to in writing, software
- ~ distributed under the License is distributed on an "AS IS" BASIS,
- ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- ~ See the License for the specific language governing permissions and
- ~ limitations under the License.
- -->
- <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
- xmlns:hr="http://mycompany.com/hr/schemas"
- elementFormDefault="qualified"
- targetNamespace="http://mycompany.com/hr/schemas">
- <xs:element name="HolidayRequest">
- <xs:complexType>
- <xs:all>
- <xs:element name="Holiday" type="hr:HolidayType"/>
- <xs:element name="Employee" type="hr:EmployeeType"/>
- </xs:all>
- </xs:complexType>
- </xs:element>
- <xs:complexType name="HolidayType">
- <xs:sequence>
- <xs:element name="StartDate" type="xs:date"/>
- <xs:element name="EndDate" type="xs:date"/>
- </xs:sequence>
- </xs:complexType>
- <xs:complexType name="EmployeeType">
- <xs:sequence>
- <xs:element name="Number" type="xs:integer"/>
- <xs:element name="FirstName" type="xs:string"/>
- <xs:element name="LastName" type="xs:string"/>
- </xs:sequence>
- </xs:complexType>
- </xs:schema>
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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
- <bean id="holidayEndpoint" class="com.mycompany.hr.ws.HolidayEndpoint">
- <constructor-arg ref="hrService" />
- </bean>
- <bean id="hrService" class="com.mycompany.hr.service.StubHumanResourceService" />
- <bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
- <property name="mappings">
- <props>
- <prop key="{http://mycompany.com/hr/schemas}HolidayRequest">
- holidayEndpoint
- </prop>
- </props>
- </property>
- <property name="interceptors">
- <bean
- class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor" />
- </property>
- </bean>
- <bean id="holiday" class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition">
- <property name="schema" ref="schema" />
- <property name="portTypeName" value="HumanResource" />
- <property name="locationUri" value="/holidayService/" />
- <property name="targetNamespace" value="http://mycompany.com/hr/definitions" />
- </bean>
- <bean id="schema" class="org.springframework.xml.xsd.SimpleXsdSchema">
- <property name="xsd" value="/WEB-INF/hr.xsd" />
- </bean>
- </beans>
web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <display-name>MyCompany HR Holiday Service</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>/*</url-pattern>
- </servlet-mapping>
- </web-app>
注意:
客户端holidayRequest.xml文件中,
......
<HolidayRequest xmlns="http://mycompany.com/hr/schemas">
......
xmlns的值必须和服务端hr.xsd文件中targetNamespace的属性值一致。
- Spring Web Service自带Demo浅析(二)
- Spring Web Service自带Demo浅析(一)
- Spring Security2.0的自带DEMO浅析
- spring boot demo( 获取一个RESTful web service)
- SharePoint自带WebService(Authentication Web Service)
- JDK6.0自带web service
- Web service jws自带测试例子
- Spring自带的JPetStore分析(二)
- Web Service(二):+ Web三层架构 (One Demo,More Questions)
- 使用XFire+Spring构建Web Service(二)
- 使用XFire+Spring构建Web Service(二)
- 使用XFire+Spring构建Web Service(二)
- 使用XFire+Spring构建Web Service(二)
- 使用XFire+Spring构建Web Service(二)
- 使用XFire+Spring构建Web Service(二)
- Web Service (二) CXF自动发布Web Service(No Spring)
- Web Service (二) CXF自动发布Web Service(No Spring)
- SharePoint自带WebService(Lists Web Service--AddAttachment Method)
- ORACLE删除表,序列发生器,索引操作
- ACE_InputCDR和ACE_OutputCDR
- 在PHP中执行系统外部命令
- 使用json-lib的JSONObject.toBean( )时碰到的日期属性转换的问题
- 国内3G牌照正式发放 三运营商分获三张牌照
- Spring Web Service自带Demo浅析(二)
- WEB前端工程师应该具备的技能
- 无题----------致那个漂浮的心
- C#实现走马灯状态栏
- C#中在IE右键菜单中添加自定义项的方法
- Makefile是什么东东?有什么用?怎么用?
- C#函数——使用SharpZipLib解压zip文件
- 有关信息抽取的文章列表(1)
- C#——使用iTextSharp生成pdf文件