******************************************************************************************
关注凡猫学院:加微信+17031115530,拉测试微信群交流
关注凡猫学院:加微信+17031115530,拉测试微信群交流
*******************************************************************************************
使用Maven 插件cxf-codegen-plugin 生成WebService 测试程序
WebService 的测试程序开发CXF 中包含了一个Maven 插件cxf-codegen-plugin,能够将Wsdl 文件动态生成webservice 本地类。下面针对Apace cxf 进行介绍如何配置,以及webservice 中的几种常见安全验证方式。Apache CXF 简介Apache CXF = Celtix + XFire。Apache CXF 是一个开源的Services 框架,CXF 利用Frontend 编程API 来构建和开发Web Services , 支持SOAP, XML/HTTP, RESTfulHTTP, 和CORBA ,并且可以在多种协议上运行,如HTTP, JMS 和JBI.官方地址:http://cxf.apache.org/将WSDL 文件转化为Java 代码CXF 中包含了一个Maven 插件cxf-codegen-plugin,能够将Wsdl 文件动态生成webservice 本地类。具体方法:在eclipse 中建立maven 项目,然后将pom 中增加配置如下,然后运行mvn run,mvn install, mvn test 等,即可生成代码。详细的pom.xml 配置文件见最下方附注。Maven 配置org.apache.cxfcxf-codegen-plugin${cxf.version}generate-sources微信+17031115530,拉测试微信群交流generate-sources${project.build.directory}http://172.0.0.1:8080/myService.wsdl${basedir}/src/main/resources/myService.wsdlwsdl2java使用上述maven 插件,代码会自动生成客户端,其名称类似于class ServiceName_ServicePort_Client,不过自动生成的客户端代码比较繁琐,不便于维护,建议自己重新建立自己的测试用例。copy 艳超的代码如下:@Testpublic void blurNameTest(){//实例化接口CompanyRemote companyRemoteClient = new CompanyRemoteImplService().//调用接口中的方法getCompanyRemoteImplPort();companyList = companyRemoteClient.getAllCompanysByBlurName(“百度”);System.out.println(companyList.size());微信+17031115530,拉测试微信群交流}WebService 客户端的安全认证WebService 客户端与服务器端进行通信时,经常需要在soap 文件中增加安全认证信息。在我的项目中,涉及到了2 种安全认证方式:1. 在soap 文件的header 中增加用户名和密码校验test1234564AJ7sPA8NRQ74faqignO3g==2013-06-20T12:33:05.001Z…方法为建立一个拦截器import org.apache.ws.security.WSPasswordCallback;import javax.security.auth.callback.Callback;import javax.security.auth.callback.CallbackHandler;import javax.security.auth.callback.UnsupportedCallbackException;微信+17031115530,拉测试微信群交流import java.io.IOException;/*** Created with IntelliJ IDEA.* User: shenyanchao* Date: 5/15/13* Time: 2:00 PM*/public class WsClientAuthHandler implements CallbackHandler {public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {for (int i = 0; i < callbacks.length; i++) {WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];pc.setIdentifier(“test”);pc.setPassword(“123456”);// ▲【这里必须设置密码】▲}}}然后在客户端中添加该拦截器FeeFinanceService_Service server=new FeeFinanceService_Service();String username = “test”;//服务器端分配的用户名String password = “123456”;//服务器端分配的密码FeeFinanceService port = server.getFeeFinanceServicePort();Client client = ClientProxy.getClient(port);Mapprops = new HashMap();微信+17031115530,拉测试微信群交流props.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);props.put(WSHandlerConstants.USER, “cxfclient”);props.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);props.put(WSHandlerConstants.PW_CALLBACK_CLASS, WsClientAuthHandler.class.getName());WSS4JOutInterceptor wss4jOut = new WSS4JOutInterceptor(props);client.getOutInterceptors().add(wss4jOut);Listreqlist = new ArrayList();JtReq req = new JtReq();req.setBusinessNumb(“T”);2. 安全验证在soap 中插入,但是实际发送soap 文件时会出现在html的头文件中POST /cmssoap/ws/fee_biz_service HTTP/1.1[\r][\n]Accept-Encoding: gzip,deflateContent-Type: text/xml;charset=UTF-8SOAPAction:Authorization: Basic dGVzdDoxMjM0NTY=Content-Length: 1475Host: 10.237.4.242:8900…该程序,可以直接在函数中增加授权验证public void testInsert() throws ValidationException_Exception {微信+17031115530,拉测试微信群交流String username = “test”;//服务器端分配的用户名String password = “123456”;//服务器端分配的密码Authenticator.setDefault(new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(“test”,”123456″.toCharArray());}});FeeBizService_Service server=new FeeBizService_Service();FeeBizService port = server.getFeeBizServicePort();BindingProvider bp = (BindingProvider)port;Mapcontext = bp.getRequestContext();context.put(BindingProvider.USERNAME_PROPERTY, username);context.put(BindingProvider.PASSWORD_PROPERTY, password);Listfees = new ArrayList();String str = port.sendFee(fees);微信+17031115530,拉测试微信群交流3. 在soap 的header 中直接增加用户名和密码youthfliesyouthflies…建立拦截器/*** @author youthflies* 自定义的soap 拦截器,用来添加header 信息*/public class SoapHeaderInterceptor extends AbstractSoapInterceptor{public SoapHeaderInterceptor(){super(Phase.WRITE);}@Overridepublic void handleMessage(SoapMessage message) throws Fault{// TODO Auto-generated method stubList headers=message.getHeaders();headers.add(getHeader(“username”, “youthflies”));微信+17031115530,拉测试微信群交流headers.add(getHeader(“password”, “youthflies”));}//http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdlprivate Header getHeader(String key, String value){QName qName=new QName(“http://webservice.webxml.com.cn/”, key);Document document=DOMUtils.createDocument();Element element=document.createElementNS(“http://webservice.webxml.com.cn/”, key);element.setTextContent(value);SoapHeader header=new SoapHeader(qName, element);return(header);}}case 中,添加拦截器://实例化接口实现类MobileCodeWS mobileCodeWS = new MobileCodeWS();//实例化接口MobileCodeWSSoap mobileCodeWSSoap = mobileCodeWS.getMobileCodeWSSoap();Client client = ClientProxy.getClient(mobileCodeWSSoap);client.getOutInterceptors().add(new SoapHeaderInterceptor());//调用接口中的方法System.out.println(mobileCodeWSSoap.getMobileCodeInfo(“13898767654”, “”));微信+17031115530,拉测试微信群交流附录一:SVN 上生成webservice 测试文件的代码路径:(请忽略SVN 地址,外面的人没法用)https://xxx.xxx.xxx/myspace/iteqa/InterfaceTest/cxf4ws生成的代码会在src/main/java 路径下面,而自己的测试代码可以放到src/test 下面,提交到SVN 时,只提交src/test 下的文件即可。完整的pom 文件:?xml version=”1.0″ encoding=”UTF-8″?4.0.0com.yourGroupIdyourArtifactId1.0-SNAPSHOT2.7.3UTF-8org.testngtestng6.8org.apache.cxf微信+17031115530,拉测试微信群交流cxf-rt-frontend-jaxws${cxf.version}org.apache.cxfcxf-rt-ws-security${cxf.version}org.apache.ws.securitywss4j1.6.10org.apache.cxfcxf-rt-bindings-soap${cxf.version}org.apache.cxfcxf-rt-transports-http${cxf.version}org.apache.maven.plugins微信+17031115530,拉测试微信群交流maven-compiler-plugin2.3.21.61.6javac1.6truetrueUTF-8org.apache.cxfcxf-codegen-plugin${cxf.version}generate-sourcesgenerate-sources${project.build.sourceDirectory}-impl-verbose-validate微信+17031115530,拉测试微信群交流http://xxx.xxx.4.242:8900/cmssoap/ws/fee
_finance_service?wsdlwsdl2java微信+17031115530,拉测试微信群交流org.eclipse.m2elifecycle-mapping1.0.0org.apache.cxfcxf-codegen-plugin[2.1.4,)wsdl2java微信+17031115530,拉测试微信群交流
******************************************************************************************
关注凡猫学院:加微信+17031115530,拉测试微信群交流
关注凡猫学院:加微信+17031115530,拉测试微信群交流
*******************************************************************************************