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.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。