简单CXF方式的webService客户端调用范例(转载)
一般webServices发布后需要测试一下,是否可行通,在此我把自己所测试的 调用 webService 的简单范例贴出来供新手参考。如有不足,请多指教。
1:需要借助的包: wss4j-1.5.4.jar cxf-bundle-2.1.3.jar
2:
package con.transnal.ucenter.client;import java.lang.reflect.Proxy;import java.util.HashMap;import java.util.Map;//import net.sxinfo.ucenter.services.UCenterService;//此处就为你发布的 webService的 源代码,此处我把它打成 jar 包了.//一般在开发中,你可以建一个webService Clietn 来引用 webService的wsdlimport org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor;import org.apache.cxf.configuration.jsse.TLSClientParameters;import org.apache.cxf.endpoint.Client;import org.apache.cxf.frontend.ClientProxy;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;import org.apache.cxf.transport.http.HTTPConduit;import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;import org.apache.ws.security.WSConstants;import org.apache.ws.security.handler.WSHandlerConstants;import org.junit.Test;/** * 客户端程序 * * @author RenWeigang */public class UCenterServiceClientTest {@Testpublic void testCreateUser() throws Exception {UCenterService ucenterService = getUCenterService();String result = ucenterService.createUser("renweigang", "123456789", "123456@test.com");System.out.println(result);}private UCenterService getUCenterService() {return getNotSSLUCenterService();}private UCenterService getNotSSLUCenterService(){JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();factory.setServiceClass(UCenterService.class);//此处的地址为对方发布的 webService的地址。factory.setAddress("http://uc.cun365.com/ucenter/ucenter/services/UCenterService");System.setProperty("org.apache.cxf.bus.factory", "org.apache.cxf.bus.CXFBusFactory");UCenterService ucenterService = (UCenterService) factory.create();ClientProxy proxy = (ClientProxy) Proxy.getInvocationHandler(ucenterService);Client client = proxy.getClient();// ③添加流模型和DOM模型转换的Handler//client.getOutInterceptors().add(new SAAJOutInterceptor());Map<String,Object> properties = new HashMap<String,Object>();// ④-1动作properties.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN + " " + WSHandlerConstants.TIMESTAMP);// PW_TEXT 明文 ,PW_DIGEST 摘要// PasswordDigest是通过非保密渠道发送用户名和口令的最佳方法。即使使用XML加密对<wsse:Password>元素进行加密,PasswordText依然可以使用properties.put(WSHandlerConstants.USER, "services-test");// ④-3指定用户properties.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);properties.put(WSHandlerConstants.PW_CALLBACK_CLASS, UtPasswordHandler.class.getName());WSS4JOutInterceptor wss4j = new WSS4JOutInterceptor(properties);client.getOutInterceptors().add(wss4j);return ucenterService;}}
<pre name="code" class="java">package net.sxinfo.ucenter.client;import java.util.HashMap;import java.util.Map;import javax.security.auth.callback.Callback;import javax.security.auth.callback.CallbackHandler;import org.apache.ws.security.WSPasswordCallback;public class UtPasswordHandler implements CallbackHandler {// ①客户端用户模拟数据库private static final Map<String,String> pwMockDB = new HashMap<String,String>(); //一般真正开发中,对方的webService指定用户,此处的 services-//test 为用户名,test 为密码。static{pwMockDB.put("services-test", "test");}public void handle(Callback[] callbacks) {WSPasswordCallback callback = (WSPasswordCallback) callbacks[0];String id = callback.getIdentifer();// ②获取用户对应的密码callback.setPassword(pwMockDB.get(id));}}</pre>