1.建立一个java工程(web工程也行)。
2.将cxf发布包中的jar导入到工程的classpath下,虽然有些包不需要,但是这样来的比较简单。
3.开始写代码了:
a. webservice接口类:
@WebService
public interface HelloWorld {
?public String sayHello(String name);
?
?public User welcome(User user);
}
?
b.webservice实现类:
@WebService
public class HelloWorldImpl implements HelloWorld {
?@Override
?public String sayHello(String name) {
??System.out.println("The method sayHello is called");
??return "hello " + name;
?}
?@Override
?public User welcome(User user) {
??System.out.println("The method welcome is called");
??List<Address> list = user.getList();
??Address ad = list.get(0);
??System.out.println("hello " + user.getId() + " - " + user.getName() + " *** " + ad.getCode() + "-" + ad.getName());
??return user;
?}
}
c.发布类测试类:
public class PublishClient {
?public static void main(String[] args){
??JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
??factory.setServiceClass(HelloWorldImpl.class);
??factory.setAddress("http://localhost:8080/webservice1");
??Server server = factory.create();
??server.start();
?}
?
}
d.测试类:
测试传输简单数据结构
@Test
?public void access(){
??JaxWsProxyFactoryBean proxy = new JaxWsProxyFactoryBean();
??proxy.setAddress("http://localhost:8080/webservice1");
??proxy.setServiceClass(HelloWorld.class);
??HelloWorld hello = (HelloWorld)proxy.create();
??System.out.println(hello.sayHello("simier"));
?}
测试传输复杂数据结构
@Test
?public void accessObj(){
??JaxWsProxyFactoryBean proxy = new JaxWsProxyFactoryBean();
??proxy.setAddress("http://localhost:8080/webservice1");
??proxy.setServiceClass(HelloWorld.class);
??HelloWorld hello = (HelloWorld)proxy.create();
??
??List<Address> list = new ArrayList<Address>();
??Address ad = new Address();
??ad.setCode(123456);
??ad.setName("hangzhou");
??
??list.add(ad);
??
??User user = new User();
??user.setId(123);
??user.setName("java_min");
??user.setList(list);
??
??User u = hello.welcome(user);
??System.out.println("It is client!");
? System.out.println("hello " + user.getId() + " - " + user.getName() + " *** " + u.getList().get(0).getCode() + "-" + u.getList().get(0).getName());??
?}
?
4.User和Address类不就用写了,普通的JavaBean.