cxf框架Demo1

来源:互联网 发布:返利机器人源码 编辑:程序博客网 时间:2024/06/05 15:41
  1. 创建java项目
  2. 引入所有jar包

  3. 创建服务类   

package com.eetrust.cxf.server;import javax.jws.WebService;import javax.xml.ws.BindingType;@WebService//制定发布soap 1.2//@BindingType(value=javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_BINDING)public class HelloService {public String sayHello(String name){return name +" hello";}}
4.发布服务

package com.eetrust.cxf.server;import org.apache.cxf.jaxws.JaxWsServerFactoryBean;public class MyCXFServer {public static void main(String[] args) {// 创建服务工厂对象// ServerFactoryBean sfb = new ServerFactoryBean(); 不推荐使用JaxWsServerFactoryBean sfb = new JaxWsServerFactoryBean();
                sfb.getInInterceptors().add(new LoggingInInterceptor());sfb.getOutInterceptors().add(new LoggingOutInterceptor());// 制定服务地址sfb.setAddress("http://127.0.0.1:8888/hello");// 绑定服务类sfb.setServiceClass(HelloService.class);// 设置服务类的实例对象sfb.setServiceBean(new HelloService());// 发布服务sfb.create();System.out.println("server ready........");}}
5.1通过wsimportSOAP1.1)生成客户端代码

             如:wsimport -s  .  http://localhost:8888/hello?wsdl

                 命令参数说明:
                   -d:生成客户端执行类的class文件的存放目录
                   -s:生成客户端执行类的源文件的存放目录
                   -p:定义生成类的包名
                    其他命令参数请参照:http://download-llnw.oracle.com/javase/6/docs/technotes/tools/share/wsimport.html
5.2 通过wsdl2java(SOAP1.1生成客户端代码

                    如:wsdl2java  –s .  http://127.0.0.1:6666/helloworld?wsdl

               点表示当前目录      url


6.调用webservice4

package com.eetrust.cxf.client;import com.eetrust.cxf.server.HelloService;import com.eetrust.cxf.server.HelloServiceService;public class SoapClient {public static void main(String[] args) {HelloServiceService hss = new HelloServiceService();HelloService hs = hss.getHelloServicePort();String sayHello = hs.sayHello("lisi");System.out.println(sayHello);}}



0 0
原创粉丝点击