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

第一个webService的实例

发布时间:2020-12-17 02:09:16 所属栏目:安全 来源:网络整理
导读:说明: 先下载apache-cxf-2.2.3.zip; 下载路径: http://apache.etoak.com/cxf/2.2.3/apache-cxf-2.2.3.zip 把?? apache-cxf-2.2.3apache-cxf-2.2.3lib下jar文件导入到项目中; ? 下面是基本的文件: ? 1.接口文件: package javaws; import javax.jws.W
说明:
先下载apache-cxf-2.2.3.zip;
下载路径: http://apache.etoak.com/cxf/2.2.3/apache-cxf-2.2.3.zip
把?? apache-cxf-2.2.3apache-cxf-2.2.3lib下jar文件导入到项目中;
?
下面是基本的文件:
?
1.接口文件:

package javaws;


import javax.jws.WebService;


@WebService

public interface HelloWorld

{

public String SayHello(String name);

}
?
2.实现接口文件:

package javaws;


import javax.jws.WebService;


@WebService

public class HelloWorldImpl implements HelloWorld

{


?? public String SayHello(String name)

??{

????System. out.println( "sevice is called!");????

???? return "hello,"+name;

??}


}
?
3.服务器端:

package javaws;

import org.apache.cxf.frontend.ServerFactoryBean;


public class MainServer

{

?? public static void main(String[] args)????

??{

????

???? HelloWorldImpl helloworldImpl = new HelloWorldImpl();

???? ServerFactoryBean svrFactory = new ServerFactoryBean();

???? svrFactory.setServiceClass(HelloWorld. class);

???? svrFactory.setAddress( "http://localhost:8888/Hello");

???? svrFactory.setServiceBean(helloworldImpl);

???? svrFactory.create();????

????????????

??}


}
4.客户端:

package javaws;

import org.apache.cxf.frontend.ClientProxyFactoryBean;


public class TestClient

{


?? public static void main(String[] args)

??{

????

???? ClientProxyFactoryBean factory = new ClientProxyFactoryBean();

???? factory.setServiceClass(HelloWorld. class);

???? factory.setAddress( "http://localhost:8888/Hello");

???? HelloWorld client = (HelloWorld)factory.create();

???? System.out.println("Invoke sayHi()....");

???? System.out.println(client.SayHello("tiger"));

????

??}


}
?
?
运行时要先启动服务器端,在运行客户端,就可以看到调用服务的效果了。
这只是入门级的体验,记录备查,有待深入学习!

(编辑:李大同)

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

    推荐文章
      热点阅读