WebService的XFire初级配置

XFrie配置笔记

       在学习之前,一直以为WebService就是一个工具,在两个服务器之间建立一个通信,帮我们把需要传输的数据组织成规范的XML数据并发送到目的地,实际情况也确实是这样的,不过更高级一点的是,XFire不但可以帮我们生成XML发送,而且可以在接收了xml之后还可以直接返回对象给我们用。

环境配置

1.    jdk1.6

2.    MyEclipse7.5

3.    jar包全部使用MyEclipse自带

步骤

我们先让WebService跑起来,看看它到底长什么样~~

1.    建立Web项目XFire

2.    配置web.xml

1)方法1:首先要配置一个contextConfigLocation,告诉Spring应该加载哪些配置文件

然后配置ContextLoaderListenerIntrospectorCleanupListener两个监听器

最后配置一个XFireSpringServlet就可以了

2)方法2:还有一种配置方式,就是使用SpringDispatcherServlet来配置XFire监听。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	version="2.4"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>XFireService</display-name>

	<!-- begin Spring配置 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/xfire-servlet.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<listener>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>
	<!-- end Spring配置 -->

	<!-- begin XFire 配置 -->
	<servlet>
		<!-- 配合Spring容器中XFire一起工作的Servlet-->
		<servlet-name>xfireServlet</servlet-name>
		<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>xfireServlet</servlet-name>
		<!--
			在这个URI下开放Web Service服务 页面访问方式为:http://localhost:8888/XFire/service
		-->
		<url-pattern>/service/*</url-pattern>
	</servlet-mapping>
	<!-- end XFire 配置 -->

	<!--
		这里提供一种使用Spring来配置XFire的方式
		页面访问方式为http://localhost:8888/XFire/HelloWorldService.ws?wsdl
		与xfire-servlet.xml中的urlMap配置相对应,两种方式可以自由选择
	-->
	<servlet>
		<servlet-name>xfire</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>xfire</servlet-name>
		<url-pattern>*.ws</url-pattern>
	</servlet-mapping>
</web-app> 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3.    配置/WEB-INFO/xfire-servlet.xml

1)方法1:这个文件用来配置具体的XFire服务,所有的服务都要在这个文件里声明。指定服务的名字和具体实现的接口和类。

2)方法2:这里也可以使用SpringSimpleUrlHandlerMapping来配置一个urlMap,与web.xml中的DispatcherServlet配合使用。

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<!-- 引入XFire预配置信息 -->
	<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" /> 
	<!-- 定义访问的url -->
	<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="urlMap">
			<map>
				<entry key="/HelloWorldService.ws">
					<ref bean="HelloWorldService" />
				</entry>
				<entry key="/DoAJobService.ws">
					<ref bean="DoAJobService" />
				</entry>
			</map>
		</property>
	</bean>

	<!-- 使用XFire导出器 -->
	<bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter"
		lazy-init="false" abstract="true">
		<!-- 引用xfire.xml中定义的工厂 -->
		<property name="serviceFactory" ref="xfire.serviceFactory" />
		<!-- 引用xfire.xml中的xfire实例 -->
		<property name="xfire" ref="xfire" />
	</bean>
	<bean id="HelloWorldService" parent="baseWebService">
		<!-- 业务服务bean -->
		<property name="serviceBean" ref="HelloWorldBean" />
		<!-- 业务服务bean的窄接口类 -->
		<property name="serviceClass" value="webservice.IHelloWorld" />
	</bean>
	<bean id="DoAJobService" parent="baseWebService">
		<property name="serviceBean" ref="DoAJobBean" />
		<property name="serviceClass" value="webservice.IDoAJob"/>
	</bean>
</beans> 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4.    配置/WEB-INF/applicationContext.xml

这个文件是Spring的配置文件,将用来实现Service的类配置在这里。

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<bean id="HelloWorldBean" class="webservice.HelloWorldImpl" />
	<bean id="DoAJobBean" class="webservice.DoAJobImpl"/>
</beans> 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

5.    引入jar

XFire服务端只需要:XFire1.2 Core Libraries

服务端还需要XFire 1.2 HTTP Client Libraries

6.    建立服务类接口IHelloWorld.java、IDoAJob.java

 

 

  

package webservice;

/**
 * WebService服务接口
 * @author zhuge
 * @create 2010-10-29 19:46:44
 */
public interface IHelloWorld {
	public String sayHelloWorld(String name);
	public String sayLove(String boy, String girl);
}

 

package webservice;

/**
 * 发送信息,让服务端输出信息
 * @author zhuge
 * @create 2010-11-06 11:37:30
 */
public interface IDoAJob {
	public void startJob(boolean needDo);
}

 

7.    建立服务实现类HelloWorldImpl.java、HelloWorldImpl.java

 

package webservice;

/**
 * WebService实现类
 * @author zhuge
 * @create 2010-10-29 19:47:38
 */
public class HelloWorldImpl implements IHelloWorld {

	/* (non-Javadoc)
	 * @see webservice.HelloWorld#sayHelloWorld(java.lang.String)
	 */
	public String sayHelloWorld(String name) {
		return "hello " + name;
	}

	/* (non-Javadoc)
	 * @see webservice.IHelloWorld#sayLove(java.lang.String, java.lang.String)
	 */
	public String sayLove(String boy, String girl) {
		return "haha, " + boy + " love " + girl;
	}

}

 

package webservice;

/**
 * @author zhuge
 * @create 
 */
public class DoAJobImpl implements IDoAJob{

	/* (non-Javadoc)
	 * @see webservice.IDoAJob#startJob(boolean)
	 */
	public void startJob(boolean needDo) {
		if(needDo) {
			System.out.println("好的,程序已经运行");
		} else {
			System.out.println("。。。。没事,发信息干啥?");
		}
	}

}

 

8.    访问测试

发布项目,运行Tomcat,访问地址:

        http://localhost:8888/XFire/service       显示本服务所有支持的服务,点击服务的wsdl进入下一个地址

        http://localhost:8888/XFire/service/IHelloWorld?wsdl 这里显示wsdl的详细内容,客户端就靠这个知道参数和返回情况。

        http://localhost:8888/XFire/service/IHelloWorld 访问这个地址,显示Invalid SOAP request 。正常,也表示webService正常

9.    建立测试客户端项目(XFireClient)

这个项目用纯Java项目建立,启动Tomcat加载了WebService之后,就可以使用这普通的java程序进行访问和通信了。

  10.建立测试类(使用wsdl文件)

使用这种访问方式,需要服务wsdlxml文件。也就是页面上显示的那个xml,可  以另存为保存下来(一般不要复制)。将后缀名改为wsdl即可。

然后使用Client类的invoke(“服务方法名”, Object[])方法即可

 

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://webservice"
	xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:tns="http://webservice"
	xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/"
	xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/"
	xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
	<wsdl:types>
		<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
			attributeFormDefault="qualified" elementFormDefault="qualified"
			targetNamespace="http://webservice">
			<xsd:element name="sayLove">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element maxOccurs="1" minOccurs="1" name="in0"
							nillable="true" type="xsd:string" />
						<xsd:element maxOccurs="1" minOccurs="1" name="in1"
							nillable="true" type="xsd:string" />
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
			<xsd:element name="sayLoveResponse">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element maxOccurs="1" minOccurs="1" name="out"
							nillable="true" type="xsd:string" />
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
			<xsd:element name="sayHelloWorld">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element maxOccurs="1" minOccurs="1" name="in0"
							nillable="true" type="xsd:string" />
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
			<xsd:element name="sayHelloWorldResponse">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element maxOccurs="1" minOccurs="1" name="out"
							nillable="true" type="xsd:string" />
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
		</xsd:schema>
	</wsdl:types>
	<wsdl:message name="sayHelloWorldResponse">
		<wsdl:part name="parameters" element="tns:sayHelloWorldResponse">
		</wsdl:part>
	</wsdl:message>
	<wsdl:message name="sayHelloWorldRequest">
		<wsdl:part name="parameters" element="tns:sayHelloWorld">
		</wsdl:part>
	</wsdl:message>
	<wsdl:message name="sayLoveResponse">
		<wsdl:part name="parameters" element="tns:sayLoveResponse">
		</wsdl:part>
	</wsdl:message>
	<wsdl:message name="sayLoveRequest">
		<wsdl:part name="parameters" element="tns:sayLove">
		</wsdl:part>
	</wsdl:message>
	<wsdl:portType name="IHelloWorldPortType">
		<wsdl:operation name="sayLove">
			<wsdl:input name="sayLoveRequest" message="tns:sayLoveRequest">
			</wsdl:input>
			<wsdl:output name="sayLoveResponse" message="tns:sayLoveResponse">
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="sayHelloWorld">
			<wsdl:input name="sayHelloWorldRequest" message="tns:sayHelloWorldRequest">
			</wsdl:input>
			<wsdl:output name="sayHelloWorldResponse" message="tns:sayHelloWorldResponse">
			</wsdl:output>
		</wsdl:operation>
	</wsdl:portType>
	<wsdl:binding name="IHelloWorldHttpBinding" type="tns:IHelloWorldPortType">
		<wsdlsoap:binding style="document"
			transport="http://schemas.xmlsoap.org/soap/http" />
		<wsdl:operation name="sayLove">
			<wsdlsoap:operation soapAction="" />
			<wsdl:input name="sayLoveRequest">
				<wsdlsoap:body use="literal" />
			</wsdl:input>
			<wsdl:output name="sayLoveResponse">
				<wsdlsoap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="sayHelloWorld">
			<wsdlsoap:operation soapAction="" />
			<wsdl:input name="sayHelloWorldRequest">
				<wsdlsoap:body use="literal" />
			</wsdl:input>
			<wsdl:output name="sayHelloWorldResponse">
				<wsdlsoap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
	</wsdl:binding>
	<wsdl:service name="IHelloWorld">
		<wsdl:port name="IHelloWorldHttpPort" binding="tns:IHelloWorldHttpBinding">
			<wsdlsoap:address location="http://localhost:8888/XFire/service/IHelloWorld" />
		</wsdl:port>
	</wsdl:service>
</wsdl:definitions>

 

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://webservice"
	xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:tns="http://webservice"
	xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/"
	xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/"
	xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
	<wsdl:types>
		<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
			attributeFormDefault="qualified" elementFormDefault="qualified"
			targetNamespace="http://webservice">
			<xsd:element name="startJob">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element maxOccurs="1" minOccurs="1" name="in0"
							type="xsd:boolean" />
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
			<xsd:element name="startJobResponse">
				<xsd:complexType />
			</xsd:element>
		</xsd:schema>
	</wsdl:types>
	<wsdl:message name="startJobRequest">
		<wsdl:part name="parameters" element="tns:startJob">
		</wsdl:part>
	</wsdl:message>
	<wsdl:message name="startJobResponse">
		<wsdl:part name="parameters" element="tns:startJobResponse">
		</wsdl:part>
	</wsdl:message>
	<wsdl:portType name="IDoAJobPortType">
		<wsdl:operation name="startJob">
			<wsdl:input name="startJobRequest" message="tns:startJobRequest">
			</wsdl:input>
			<wsdl:output name="startJobResponse" message="tns:startJobResponse">
			</wsdl:output>
		</wsdl:operation>
	</wsdl:portType>
	<wsdl:binding name="IDoAJobHttpBinding" type="tns:IDoAJobPortType">
		<wsdlsoap:binding style="document"
			transport="http://schemas.xmlsoap.org/soap/http" />
		<wsdl:operation name="startJob">
			<wsdlsoap:operation soapAction="" />
			<wsdl:input name="startJobRequest">
				<wsdlsoap:body use="literal" />
			</wsdl:input>
			<wsdl:output name="startJobResponse">
				<wsdlsoap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
	</wsdl:binding>
	<wsdl:service name="IDoAJob">
		<wsdl:port name="IDoAJobHttpPort" binding="tns:IDoAJobHttpBinding">
			<wsdlsoap:address location="http://localhost:8888/XFire/service/IDoAJob" />
		</wsdl:port>
	</wsdl:service>
</wsdl:definitions>

 

package test;

import java.io.IOException;

import org.codehaus.xfire.client.Client;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * 测试连接WebService服务端,使用WSDL配置文件
 * @author zhuge
 * @create 2010-11-06 10:39:15
 */
public class ClientWithWsdl {
	public static void main(String[] args) {
		testHello();
		testJob();
	}

	private static void testJob() {
		try {
			String wsdl = "test/IDoAJob.wsdl";
			Resource resource = new ClassPathResource(wsdl);
			Client client = new Client(resource.getInputStream(), null);
			client.invoke("startJob", new Object[]{true});
			client.invoke("startJob", new Object[]{false});
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void testHello() {
		try {
			String wsdl = "test/IHelloWorld.wsdl";
			Resource resource = new ClassPathResource(wsdl);
			Client client = new Client(resource.getInputStream(), null);
			Object[] results = client.invoke("sayHelloWorld", new Object[]{"诸葛"});
			System.out.println(results[0]);
			
			//测试多个参数传递
			Object[] results2 = client.invoke("sayLove", new Object[]{"Jack", "Rose"});
			for(int i = 0; i < results2.length; i++) {
				System.out.println(results2[i]);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

11.建立测试类(使用xml地址)

a)      添加一个client.xml

在这个文件中配置好WebService的地址。可以访问,不过目前测试时需要将服务端的类copy过来,我还没有发现这种方式的可用之处。不填代码了

  b)      测试类

使用Spring getBean之后,直接操作。。。。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值