启动eclipse 新建一个工程test如图:
新建一个对象Account,供webservices调用
package webServices;
public class Account {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
新建一个接口
package webServices;
public interface MathService {
Account sayHello(Account account);
}
实现类
package webServices;
public class MathServiceImpl implements MathService{
@Override
public Account sayHello(Account account) {
account.setName("hello"+account.getName());
return account;
}
}
新建WEB-INF/spring.xml,一个简单bean配置
<?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="mathBean" class="webServices.MathServiceImpl"/>
</beans>
新建WEB-INF/xfire-servlet.xml,webservice配置相关信息
<?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
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/MathService">
<ref bean="math" />
</entry>
</map>
</property>
</bean>
<bean id="math"
class="org.codehaus.xfire.spring.remoting.XFireExporter">
<property name="serviceFactory">
<ref bean="xfire.serviceFactory" />
</property>
<property name="xfire">
<ref bean="xfire" />
</property>
<!-- spring配置实现接口类-->
<property name="serviceBean">
<ref bean="mathBean" />
</property>
<!-- 接口类-->
<property name="serviceClass">
<value>webServices.MathService</value>
</property>
</bean>
</beans>
修改web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>test</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring.xml
classpath:/org/codehaus/xfire/spring/xfire.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</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>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
启动web应用访问http://localhost:8080/test/MathService?wsdl可以看见webservice配置信息
JAVA客户端测试
package client;
import java.net.MalformedURLException;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import webServices.Account;
import webServices.MathService;
public class Client {
/** *//**
* @param args
*/
public static void main(String[] args){
String serviceURL="http://localhost:8080/test/MathService";
Service serviceModel = new ObjectServiceFactory().create(MathService.class,null,serviceURL,null);
XFireProxyFactory serviceFactory = new XFireProxyFactory();
MathService service = null;
try {
service = (MathService) serviceFactory.create(serviceModel, serviceURL);
Account account=new Account();
account.setName("example");
System.out.println(service.sayHello(account).getName());
} catch (MalformedURLException e){
e.printStackTrace();
}
}
}
vs2005里面调用增加web引用
导入相关类,直接new 用.
import example.localhost.*;
AccountDao accountService = new AccountDao();
textBox3.set_Text(accountService.sayHello("hhhhh"));