加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

使用CXF框架实现webservice的一个简单例子

发布时间:2020-12-17 00:58:04 所属栏目:安全 来源:网络整理
导读:1.建立一个java工程(web工程也行)。 2.将cxf发布包中的jar导入到工程的classpath下,虽然有些包不需要,但是这样来的比较简单。 3.开始写代码了: a. webservice接口类: @WebService public interface HelloWorld { ?public String sayHello(String name);

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读