public interface HelloWorld {
String sayHelloWorld(String name);
}
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;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class WebServiceClientTest {
HelloWorld helloWorld = null;
public static void main(String[] args) {
WebServiceClientTest test = new WebServiceClientTest();
test.testClient();
}
public void testClient() {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"client.xml");
helloWorld = (HelloWorld) ctx.getBean("testWebService");
System.out.println(helloWorld.sayHelloWorld("阿蜜果"));
}
public String callWebService()
throws MalformedURLException, Exception {
Service serviceModel = new ObjectServiceFactory().create(HelloWorld.class);
XFire xfire = XFireFactory.newInstance().getXFire();
XFireProxyFactory factory = new XFireProxyFactory(xfire);
String serviceUrl = "http://localhost:8080/WSTest/HelloWorldService.ws";
HelloWorld client = null;
try {
client = (HelloWorld) factory.create(serviceModel, serviceUrl);
} catch (MalformedURLException e) {
}
//Invoke the service
String serviceResponse = "";
try {
serviceResponse = client.sayHelloWorld("小明");
} catch (Exception e){
serviceResponse = e.toString();
}
return serviceResponse;
}
public static void main2(String args[])
{
WebServiceClientTest client = new WebServiceClientTest();
try {
String result=client.callWebService();
System.out.println("调用webservice后的结果为:"+result);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<?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>HelloWorld</value>
</property>
<property name="wsdlDocumentUrl">
<value>http://localhost:8080/WSTest/HelloWorldService.ws?wsdl</value>
</property>
</bean>
</beans>
服务端
<?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>cmms</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/applicationContext*.xml,/WEB-INF/xfire-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring 刷新Introspector防止内存泄露 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<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>
<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服务 -->
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<!-- end XFire 配置 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</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 class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/HelloWorldService.ws">
<ref bean="HelloWorldService" />
</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.HelloWorld" />
</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" class="webservice.HelloWorldImpl"/>
</beans>
package webservice;
public interface HelloWorld {
String sayHelloWorld(String name);
}
package webservice;
public class HelloWorldImpl implements HelloWorld {
@Override
public String sayHelloWorld(String name) {
String helloWorld = "hello," + name;
return helloWorld;
}
}
客户端最小2类包:
jdom.jar
wsdl4j-1.5.2.jar
xfire-all-1.1.2.jar
commons-logging-1.0.2.jar
commons-codec-1.2.jar
commons-httpclient-3.0.jar