1 使用CXF框架开发 服务器端
package org.shi.cxf.ws;
import javax.jws.WebService;
/**
* 需要暴露服务的接口
* @author xiaoshi
*
*/
@WebService
public interface HelloWorld {
public String sayHi(String name);
}
package org.shi.cxf.ws.impl;
import java.util.Date;
import javax.jws.WebService;
/**
* 需要暴露服务的实现
* @author xiaoshi
*
*/
@WebService(endpointInterface="org.shi.cxf.ws.HelloWorld",serviceName="HelloWorldServiceName")
public class HelloWorldImpl implements org.shi.cxf.ws.HelloWorld {
@Override
public String sayHi(String name) {
// TODO Auto-generated method stub
return name +",您好。欢迎来到CXF世界!!! 现在时间是:" + new Date();
}
}
package org.shi.cxf;
import javax.xml.ws.Endpoint;
import org.shi.cxf.ws.HelloWorld;
import org.shi.cxf.ws.impl.HelloWorldImpl;
/**
* Web Service 服务器端 主启动类
* @author xiaoshi
*
*/
public class WSServiceStart {
public static void main(String[] args) {
//需要暴露的服务
HelloWorld hw = new HelloWorldImpl();
// 调用Endpoint 的 publish方法发布 Web Service 服务
Endpoint.publish("http://127.0.0.1/shiWS", hw);
System.out.println("web Service 发布成功!");
}
}
导入需要的jar包 到项目中
2 使用CXF框架开发 客户端
新建项目,切换cmd 到新建项目的src目录下 执行 (然后刷新项目)
wsdl2java http://127.0.0.1/shiWS?wsdl
然后直接可以写客户端的启动类
package org.shi.cxf;
import org.shi.cxf.ws.HelloWorld;
import org.shi.cxf.ws.impl.HelloWorldServiceName;
/**
* web service 客户端 启动方法
* @author xiaoshi
*
*/
public class WSClientStart {
public static void main(String[] args) {
HelloWorldServiceName servieFactory = new HelloWorldServiceName();
//此处返回的只是远程Web Service的代理;
HelloWorld hw = servieFactory.getHelloWorldImplPort();
System.out.println(hw.sayHi("施爷"));
}
}
服务器端拦截器的简单实现
package org.shi.cxf;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import javax.xml.ws.Endpoint;
import org.apache.cxf.ext.logging.LoggingOutInterceptor;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.jaxws.EndpointImpl;
import org.shi.cxf.ws.HelloWorld;
import org.shi.cxf.ws.QueryCatsByUser;
import org.shi.cxf.ws.impl.HelloWorldImpl;
import org.shi.cxf.ws.impl.QueryCatsByUserImpl;
/**
* Web Service 服务器端 主启动类
* @author xiaoshi
*
*/
public class WSServiceStart {
public static void main(String[] args) throws FileNotFoundException {
//需要暴露的服务
HelloWorld hw = new HelloWorldImpl();
QueryCatsByUser queryCatsByUser = new QueryCatsByUserImpl();
// 调用Endpoint 的 publish方法发布 Web Service 服务
EndpointImpl ep = (EndpointImpl) Endpoint.publish("http://127.0.0.1/shiWS", hw);
//添加In(输入)拦截器
//ep.getInInterceptors().add(new LoggingInInterceptor(new PrintWriter("in.txt")));
ep.getInInterceptors().add(new LoggingInInterceptor());
//添加Out(输出)拦截器
//ep.getOutInterceptors().add(new LoggingInInterceptor(new PrintWriter("out.txt")));
ep.getOutInterceptors().add(new LoggingOutInterceptor());
System.out.println("web Service 发布成功!");
}
}
客户端拦截器