1 使用CXF框架開發 服務器端java
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包 到項目中web
2 使用CXF框架開發 客戶端 apache
新建項目,切換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("施爺")); } }
服務器端攔截器的簡單實現ide
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 發佈成功!"); } }
客戶端攔截器代理