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);

 

 

你可能感兴趣的:(CXF)