WebService的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>
<?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 /></entry><entry key="/DoAJobService.ws"><ref bean="DoAJobService" /></entry></map></property></bean><!-- 使用XFire导出器 --><bean id="baseWebService" 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>
<?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" /><bean id="DoAJobBean" name="code">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);}
?
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("。。。。没事,发信息干啥?");}}}
<?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();}}}
?