/** * @author xp * @Title: ClientMain.java * @Package test * @Description: TODO * @date 2016年5月1日 下午6:39:44 * @version V1.0 */ package test; import java.util.List; import org.apache.cxf.endpoint.Client; import org.apache.cxf.feature.LoggingFeature; import org.apache.cxf.frontend.ClientProxy; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import com.xp.cn.ws.Cat; import com.xp.cn.ws.Entry; import com.xp.cn.ws.IWebServiceDemo; import com.xp.cn.ws.StringCat; import com.xp.cn.ws.User; import com.xp.cn.ws.impl.WebServiceImpl; /** * @author xp * @ClassName: ClientMain * @Description: TODO * @date 2016年5月1日 下午6:39:44 * */ public class ClientMain { public static void main(String[] args) { //繼承Service的類當成工廠使用 WebServiceImpl webServiceImpl = new WebServiceImpl(); //此處返回的只是遠程webservice的代理 IWebServiceDemo webService = webServiceImpl.getWebServiceImplPort(); //客戶端攔截器寫在調用服務端方法以前 //org.apache.cxf.frontend.ClientProxy.getClient(); Client client = ClientProxy.getClient(webService); client.getInInterceptors().add(new LoggingInInterceptor()); client.getOutInterceptors().add(new LoggingOutInterceptor()); //客戶端攔截器寫在調用服務端方法以前 String sayHello = webService.sayHello("xp"); System.out.println(sayHello); // User user = new User(); // user.setName("jaychou"); // user.setPassword("jaychou"); // List<Cat> catsByUser = webService.getCatsByUser(user); // for (Cat cat : catsByUser) { // System.out.println(cat.getName()); // System.out.println(cat.getColor()); // } // StringCat allCats = webService.getAllCats(); // for (Entry cats : allCats.getEntries()) { // System.out.println(cats.getKey() + "==" + cats.getValue().getName()); // } } }
控制檯的日誌信息以下:java
五月 02, 2016 8:16:20 下午 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL 信息: Creating Service {http://impl.ws.cn.xp.com/}webServiceImpl from WSDL: http://127.0.0.1/XXX?wsdl 五月 02, 2016 8:16:20 下午 org.apache.cxf.services.webServiceImpl.WebServiceImplPort.IWebServiceDemo 信息: Outbound Message --------------------------- ID: 1 Address: http://127.0.0.1/XXX Encoding: UTF-8 Http-Method: POST Content-Type: text/xml Headers: {Accept=[*/*], SOAPAction=[""]} Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHello xmlns:ns2="http://ws.cn.xp.com/"><arg0>xp</arg0></ns2:sayHello></soap:Body></soap:Envelope> -------------------------------------- 五月 02, 2016 8:16:20 下午 org.apache.cxf.services.webServiceImpl.WebServiceImplPort.IWebServiceDemo 信息: Inbound Message ---------------------------- ID: 1 Response-Code: 200 Encoding: UTF-8 Content-Type: text/xml;charset=UTF-8 Headers: {content-type=[text/xml;charset=UTF-8], Server=[Jetty(8.1.14.v20131031)], transfer-encoding=[chunked]} Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHelloResponse xmlns:ns2="http://ws.cn.xp.com/"><return>你好xpMon May 02 20:16:20 CST 2016</return></ns2:sayHelloResponse></soap:Body></soap:Envelope> -------------------------------------- 你好xpMon May 02 20:16:20 CST 2016