1、创建好一个web工程,引入xfire下的jar包,注意lib下的和xfire-all.jar
2、定义接口;
- package com.iteye.webservice;
- public interface IHelloService {
- public String sayHelleWorld(String inputStr);
- }
3、接口实现;
- package com.iteye.webservice;
- public class HelloImpl implements IHelloService {
- @Override
- public String sayHelleWorld(String name) {
- System.out.println("hello worl! this is first webservice!!!");
- return "Hello "+name;
- }
- }
4、web.xml配置访问公开接口信息;
以下配置都是固定的配置信息:
- <servlet>
- <servlet-name>XFireServlet</servlet-name>
- <servlet-class>
- org.codehaus.xfire.transport.http.XFireConfigurableServlet
- </servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>XFireServlet</servlet-name>
- <url-pattern>/servlet/XFireServlet/*</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
- <servlet-name>XFireServlet</servlet-name>
- <url-pattern>/services/*</url-pattern>
- </servlet-mapping>
5、新建META-INF,新建xfire,新建services.xml,并配置接口信息;
结构:/src/META-INF/xfire/services.xml
servics.xml内容:
- <?xml version="1.0" encoding="UTF-8" ?>
- <beans xmlns="http://xfire.codehaus.org/config/1.0">
- <service>
- <name>HelloService</name>
- <namespace>http://com/iteye/webservice/HelloService</namespace>
- <serviceClass>com.iteye.webservice.IHelloService</serviceClass>
- <implementationClass>com.iteye.webservice.HelloImpl</implementationClass>
- </service>
-
- </beans>
6、部署访问,生成
?wsdl文件。
加载到tomcat中,启动浏览器地址栏输入:
http://localhost:8080/webservicedemo/services
返回?wsdl文件。
7、模拟远程调用服务器端
创建一个客户端项目(Java工程或者web工程),引入jar包,创建客户端代码:
1、client客户端
- @Test
- public void testSayHelle() {
- try {
- Client client = new Client(new URL(
- "http://localhost:8090/webservicedemo/services/HelloService?wsdl"));
- Object[] objects = client.invoke("sayHelleWorld",
- new Object[] { "tom" });
- System.out.println(objects[0]);
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
2、xfire代理客户端
- @Test
- public void testFactoryClient() throws MalformedURLException {
-
- ObjectServiceFactory serviceFactory = new ObjectServiceFactory();
-
- Service service = serviceFactory.create(IHelloService.class);
-
- XFireProxyFactory fireProxyFactory = new XFireProxyFactory();
-
- IHelloService hs = (IHelloService) ireProxyFactory.create(service,
- "http://localhost:8080/webservicedemo/services/HelloService");
-
- hs.sayHelleWorld("tom");