首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

运用CXF开发WebService服务器端和客户端

2013-01-23 
使用CXF开发WebService服务器端和客户端开发需要的jar包:服务器端代码:package com.cxf.ws.serviceimport

使用CXF开发WebService服务器端和客户端
开发需要的jar包:

服务器端代码:

package com.cxf.ws.service;import javax.jws.WebService;@WebServicepublic interface HelloWorld {public String sayHello(String name);}

package com.cxf.ws.service.impl;import javax.jws.WebService;import com.cxf.ws.service.HelloWorld;@WebService(endpointInterface="com.cxf.ws.service.HelloWorld",serviceName="HelloWorldImpl")public class HelloWorldImpl implements HelloWorld{@Overridepublic String sayHello(String name) {return "hello,"+name+"!";}}

package com.cxf.ws.app;import javax.xml.ws.Endpoint;import com.cxf.ws.service.HelloWorld;import com.cxf.ws.service.impl.HelloWorldImpl;public class Server {public static void main(String[] args) {//JaxWsServerFactoryBean factoryBean=new JaxWsServerFactoryBean();//factoryBean.setAddress("http://192.168.0.251:9999/CXF-WS-Server");//factoryBean.setServiceClass(HelloWorld.class);//factoryBean.setServiceBean(new HelloWorldImpl());//factoryBean.create();HelloWorld hw=new HelloWorldImpl();Endpoint.publish("http://192.168.0.251:9999/CXF-WS-Server", hw);System.out.println("发布成功!");}}


配置wsdl2java环境:
path:D:\apache-cxf-2.5.8\bin
cmd 命令:
切换目录到客户端工程的src下
wsdl2java http://192.168.0.251:9999/CXF-WS-Server/HelloWorld?wsdl
客户端代码:
package com.cxf.ws.app;import com.cxf.ws.service.HelloWorld;import com.cxf.ws.service.impl.HelloWorldImpl;public class Client {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubHelloWorldImpl fac=new HelloWorldImpl();HelloWorld hw=fac.getHelloWorldImplPort();String word=hw.sayHello("金聪敏");System.out.println(word);}}

热点排行