`

CXF Webservice(一) 简单实例

    博客分类:
  • CXF
阅读更多

一、下载CXF的jar包:所需要的jar包

二、创建Server端,创建CXF_Server项目

  1、导入所需根据jar包

  2、新建一个接口

import javax.jws.WebService;
@WebService
public interface HelloWorld {
      public String sayHi(String name);
}

   3、新建一个接口实现类

@WebService(endpointInterface="com.tzz.cxf.ws.HelloWorld",serviceName="HelloWorldWs")
public class HelloWorldWs implements HelloWorld{

	@Override
	public String sayHi(String name) {
		String str = name + ",您好,现在时间是"+new Date();
		System.out.println("server---------"+str);
		return str;
	}

}

  说明: 

  @WebService:申明为webservice的注解 
  endpointInterface:要暴露的接口类 
  serviceName :服务名,其实就是调用地址 

  4、发布服务

public class ServerMain {
	public static void main(String[] args) {
		HelloWorld helloWorld = new HelloWorldWs();
		Endpoint.publish("http://127.0.0.1/helloworld:8082", helloWorld); 
	}
}

  5、运用ServerMain类启动服务,浏览器输入http://127.0.0.1/helloworld:8082?wsdl地址,访问成功,说明服务启动成功,CXF服务端构造完成

 

三、创建Client端,创建CXF_Client项目

   1、将下载的CXF中的bin加入到系统环境变量path中:

    1.1、新建变量CXF_HOME,CXF_HOME=“E:\\apache-cxf-.7.14”

    1.2、在path中加入;%CXF_HOME%\bin

    2、利用wsdl2java生成客服端代码

  3、Client端调用Server端的服务

public class ClientMain {
	public static void main(String[] args) {
		HelloWorldWs helloWorldWs =  new HelloWorldWs();
		HelloWorld helloWorld = helloWorldWs.getHelloWorldWsPort();
		String str = helloWorld.sayHi("test");
		System.out.println("Client---------"+str);
	}
}

  4、运行ClientMain类

  5、输出结果

  Client---------test,您好,现在时间是Fri Jan 16 21:58:12 CST 2015
 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics