最近重新熟悉下webservice知识,以前用的是axis2,现在用另一种方式spring xfire来实现简单的webservice;
xfire和axis
xfire比axis性能高
axis比xfire响应时间短
一、在eclipse下新建项目工程xfire
二、导入基本的jar包
commons-codec-1.3.jar
commons-httpclient-3.0.jar
commons-logging-1.0.4.jar
jdom-1.0.jar
jsr181-api.jar
spring.jar
wsdl4j-1.6.1.jar
xfire-all-1.2.6.jar
XmlSchema-1.4.7.jar
三、web.xml配置
四、xfire-servlet.xml
五、
接口
实现类
拦截器
客户端测试
TestClientByXml.java
xfire和axis
xfire比axis性能高
axis比xfire响应时间短
一、在eclipse下新建项目工程xfire
二、导入基本的jar包
commons-codec-1.3.jar
commons-httpclient-3.0.jar
commons-logging-1.0.4.jar
jdom-1.0.jar
jsr181-api.jar
spring.jar
wsdl4j-1.6.1.jar
xfire-all-1.2.6.jar
XmlSchema-1.4.7.jar
三、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>xfire</display-name>
- <!-- 定义装入的spring配置文件 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- classpath:org/codehaus/xfire/spring/xfire.xml
- /WEB-INF/xfire-servlet.xml
- </param-value>
- </context-param>
- <!-- 自动装配配置信息 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- 它主要负责处理由 JavaBean Introspector 功能而引起的缓存泄露 -->
- <listener>
- <listener-class>
- org.springframework.web.util.IntrospectorCleanupListener
- </listener-class>
- </listener>
- <!-- 注意因为servlet-name为xfire,固xfire配置文件名应该是xfire-servlet.xml -->
- <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>
- <!-- 配合Spring容器中XFire一起工作的Servlet- -->
- <servlet>
- <servlet-name>xfireServlet</servlet-name>
- <servlet-class>
- org.codehaus.xfire.spring.XFireSpringServlet
- </servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>xfireServlet</servlet-name>
- <url-pattern>/services/*</url-pattern>
- </servlet-mapping>
- </web-app>
四、xfire-servlet.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <!-- basic configuration -->
- <!-- 若是web.xml已经配置了org/codehaus/xfire/spring/xfire.xml,这里就不需要配置了 -->
- <!-- <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" /> -->
- <bean id="baseExporter" class="org.codehaus.xfire.spring.remoting.XFireExporter"
- lazy-init="false" abstract="true">
- <property name="serviceFactory" ref="xfire.serviceFactory" />
- <property name="xfire" ref="xfire" />
- <property name="outHandlers">
- <list>
- <ref bean="domOutHandler"/>
- <ref bean="soapOutHandler"/>
- </list>
- </property>
- <property name="inHandlers">
- <list>
- <ref bean="domInHandler"/>
- </list>
- </property>
- </bean>
- <bean id="domOutHandler" class="org.codehaus.xfire.util.dom.DOMOutHandler" />
- <bean id="soapOutHandler" class="com.bing.webservice.SoapOutHandler" />
- <bean id="domInHandler" class="org.codehaus.xfire.util.dom.DOMInHandler" />
- <!-- basic configuration -->
- <!-- 定义访问的url,但是web.xml需要配置org.springframework.web.servlet.DispatcherServlet-->
- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- <property name="urlMap">
- <map>
- <entry key="/hello.ws">
- <ref bean="helloExporter"/>
- </entry>
- </map>
- </property>
- </bean>
- <!-- hello webservice -->
- <bean id="helloExporter" parent="baseExporter">
- <property name="serviceBean" ref="hello" />
- <property name="serviceClass" value="com.bing.webservice.IHello" />
- <property name="name" value="sayHello"/>
- </bean>
- <bean id="hello" class="com.bing.webservice.HelloImpl" />
- <!-- hello webservice -->
- </beans>
五、
接口
- package com.bing.webservice;
- public interface IHello {
- public String sayHello(String name);
- }
实现类
- package com.bing.webservice;
- public class HelloImpl implements IHello {
- public String sayHello(String name) {
- return "hello! "+ name;
- }
- }
拦截器
- package com.bing.webservice;
- import org.codehaus.xfire.MessageContext;
- import org.codehaus.xfire.handler.AbstractHandler;
- public class SoapOutHandler extends AbstractHandler {
- public void invoke(MessageContext mc) throws Exception {
- System.out.println(mc.getCurrentMessage());
- }
- }
客户端测试
- package com.bing.webservice;
- import java.net.MalformedURLException;
- import org.codehaus.xfire.XFire;
- import org.codehaus.xfire.XFireFactory;
- import org.codehaus.xfire.client.XFireProxyFactory;
- import org.codehaus.xfire.service.Service;
- import org.codehaus.xfire.service.binding.ObjectServiceFactory;
- public class TestClient {
- public static void main(String[] args) {
- Service serviceModel = new ObjectServiceFactory().create(IHello.class);
- XFire xfire=XFireFactory.newInstance().getXFire();
- XFireProxyFactory factory=new XFireProxyFactory(xfire);
- String serviceUrl = "http://localhost:8080/xfire/services/sayHello";
- IHello ds= null;
- try {
- ds=(IHello)factory.create(serviceModel, serviceUrl);
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- System.out.println(ds.sayHello("Tom"));
- }
- }
另一种方式调用:
client.xml 放在src目录下
- <?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 ="testWebService" class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean">
- <property name="serviceClass">
- <value>com.bing.webservice.IHello</value>
- </property>
- <property name="wsdlDocumentUrl">
- <value>
- http://localhost:8080/xfire/services/sayHello?wsdl
- </value>
- </property>
- </bean>
- </beans>
TestClientByXml.java
- package com.bing.webservice;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class TestClientByXml {
- /**
- */
- public static void main(String[] args) {
- ApplicationContext ac=new ClassPathXmlApplicationContext("client.xml");
- IHello hello=(IHello)ac.getBean("testWebService");
- hello.sayHello("lubing");
- }
- }