1、 客户端访问
客户端接口:
/**
* @author: 團長
* @date: 2013-2-4-下午1:09:12
*/
package wsClient;
/**
* TODO:
*
* @author 團長
* @since 2013-2-4
*/
public interface ClientInt {
public String ask(String question);
}
客户端实现:
/**
* @author: 團長
* @date: 2013-2-4-下午1:10:20
*/
package wsClient;
import org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.Conduit;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import ws.WsInt;
/**
* TODO:
*
* @author 團長
* @since 2013-2-4
*/
public class ClientImpl implements ClientInt {
private WsInt ws;
private String address;
public ClientImpl() {
this.address = "http://localhost:7001/TestWS/service/qa";
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(WsInt.class);
factory.setAddress(address);
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.getOutInterceptors().add(new SAAJOutInterceptor());
ws = (WsInt) factory.create();
Client clientl = ClientProxy.getClient(ws);
Conduit conduit = clientl.getConduit();
HTTPConduit hc = (HTTPConduit) conduit;
HTTPClientPolicy httpClient = new HTTPClientPolicy();
hc.setClient(httpClient);
}
/**
* TODO:
*
* @author 團長
* @since 2013-2-4
* @param question
* @return
* @see wsClient.ClientInt#ask(java.lang.String)
*
*/
@Override
public String ask(String question) {
String result = null;
result = ws.qa(question);
return result;
}
}
客户端测试:
/**
* @author: 團長
* @date: 2013-2-4-下午1:15:37
*/
package test;
import org.junit.Test;
import wsClient.ClientImpl;
import wsClient.ClientInt;
/**
* TODO:
*
* @author 團長
* @since 2013-2-4
*/
public class TestClient {
@Test
public void test() {
ClientInt client = new ClientImpl();
String result = client.ask("ni shi shei");
System.out.println(result);
}
}
运行junit测试用例,应输出:
信息: Outbound Message
---------------------------
ID: 1
Address: http://localhost:7001/TestWS/service/qa
Encoding: UTF-8
Content-Type: text/xml
Headers: {SOAPAction=[""], Accept=[*/*]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:qa xmlns:ns2="http://ws/"><arg0>ni shi shei</arg0></ns2:qa></soap:Body></soap:Envelope>
--------------------------------------
this is my life
2、 这是用Eclipse创建cxf webservice的简易方法。没有涉及到wsdl、cxf的用户安全验证等细节。这些东西请自己补充。
本文转自 斯然在天边 51CTO博客,原文链接:http://blog.51cto.com/winters1224/1131748,如需转载请自行联系原作者