cxf 调用webservice

服务器端发布的service地址为:http://localhost:8080/webP/webservice/helloWorld

1)在客户端先访问下,看能否访问到,在服务器发布的地址后面加上URL+?wsdl;如:http://localhost:8080/webP/webservice/helloWorld?wsdl 访问。

如果看到xml文档信息,说明正常。

2)在dos下输入cmd运行,找到解压的apache-cxf-2.6.1的bin目录下, 运行

wsdl2java -p ws http://localhost:8080/webP/webservice/helloWorld?wsdl 在apache-cxf-2.6.1的bin目录就生成了ws文件夹


3)把生成的ws文件夹中的java文件放到自己的项目中,这些java文件就是调用webservice所需要的类

4)运行测试类

import java.util.ArrayList;
import java.util.List;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import ws.HelloWorld;
import ws.User;
 
public class HelloWorldClient {
 
 public static void main(String[] args) {
  JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
  svr.setServiceClass(HelloWorld.class);
  svr.setAddress("http://localhost:8080/webP/webservice/helloWorld");
  HelloWorld hw = (HelloWorld) svr.create();
  User user = new User();
  user.setName("Tony1");
  System.out.println(hw.sayHiToUser(user));
  System.out.println(hw.sayHi("wang"));
  
  List<User> list=new ArrayList<User>();
  list.add(user);
  System.out.println(hw.sayHiToUserList(list));
 }
}

输出结果,over!

转自:http://blog.sina.com.cn/s/blog_8ee5914d01016ctl.html

猜你喜欢

转载自xueqi.iteye.com/blog/1779306