cxf框架Demo1

  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 通过wsimport SOAP1.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);
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值