Cxf Hello

1.定义接口

import javax.jws.WebService;

@WebService
public interface HelloWorld
{

	String sayHi(String text);
}

 2.实现

//@WebService(endpointInterface = "XXXXX.HelloWorld", serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld
{
	 
	public String sayHi(String text)
	{ 
		return "Hello " + text;
	}
}

 3.Server

import javax.xml.ws.Endpoint;

HelloWorldImpl implementor = new HelloWorldImpl();
String address = "http://localhost:9000/helloWorld";
Endpoint.publish(address, implementor);

 4.client

import java.net.URL; 

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

private static final String name = "http://server.hello.demo.cxf.zhengchao.net.cn/";

private static final QName SERVICE_NAME = new QName(name, "HelloWorld");


URL WSDL_LOCATION = new URL("http://localhost:9000/helloWorld?wsdl");
Service service = Service.create(WSDL_LOCATION, SERVICE_NAME);
HelloWorld hw = service.getPort(HelloWorld.class);
System.out.println(hw.sayHi("World"));

 

 方法二

Factory Server

import org.apache.cxf.frontend.ServerFactoryBean;

HelloWorldImpl helloworldImpl = new HelloWorldImpl();
ServerFactoryBean svrFactory = new ServerFactoryBean();
svrFactory.setServiceClass(HelloWorld.class);
svrFactory.setAddress("http://localhost:9000/Hello");
svrFactory.setServiceBean(helloworldImpl);
// svrFactory.getServiceFactory().setDataBinding(new
// AegisDatabinding());
svrFactory.create();

 Factory Client

import org.apache.cxf.frontend.ClientProxyFactoryBean;

ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
factory.setAddress("http://localhost:9000/Hello");
HelloWorld client = factory.create(HelloWorld.class);

 

添加Interceptor

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

HelloWorldImpl implementor = new HelloWorldImpl();
JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
svrFactory.setServiceClass(HelloWorld.class);
svrFactory.setAddress("http://localhost:9000/helloWorld");
svrFactory.setServiceBean(implementor);
svrFactory.getInInterceptors().add(new LoggingInInterceptor());
svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
svrFactory.create();

 client

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.setAddress("http://localhost:9000/helloWorld");
HelloWorld client = factory.create(HelloWorld.class);

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值