此文章是sap系统提供WebService服务端,使用Maven工程,自动生成WebService客户端代码,并设置http basic认证,进行调用,得到服务端的返回。
GET /index.html HTTP/1.1
Host: server001.sap.com
Authorization: Basic aGFyYm9yOmhhcmJvcg==
其中aGFyYm9yOmhhcmJvcg==为 用户名:密码 进行base64加密后,utf-8编码的数据
在线base64加密:Base64在线编码解码 UTF-8http://tools.jb51.net/tools/base64_decode-utf8.php
根据导出WSDL文件,或者在线的WSDL地址,通过maven的cxf-codegen-plugin插件,生成WebService客户端的代码。
允许mvn命令,指定使用cxf-codegen-plugin插件,指定goal为wsdl2java
mvn org.apache.cxf:cxf-codegen-plugin:3.2.13:wsdl2java
生成代码输出至interface-ws-sap\target\generated-sources\cxf\com\sap\document\sap\rfc\functions 目录
然后将代码拷贝至工程的正确代码位置
其中ZFMSDCRMFUND为持有ZSSDCRMFUND的item元素的ArrayList对象。
最终调用的代码为
com.sap.sales.wsdl.fund.ObjectFactory objectFactory = new com.sap.sales.wsdl.fund.ObjectFactory();
String url = sapWsAddress+"/XISOAPAdapter/MessageServlet?senderParty=&senderService=BS_CRM&receiverParty=&receiverService=&interface=SI_FUND_OB_SYNC&interfaceNamespace=http://crm.sap.com/interfaces/FUND";
com.sap.sales.wsdl.fund.ZFMSDCRMFUND requestDTO = sapFundService.getData();
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
// this is the package name specified in the <generatePackage> specified in pom.xml
marshaller.setContextPath("com.sap.sales.wsdl.fund");
JAXBElement<com.sap.sales.wsdl.fund.ZFMSDCRMFUNDResponse> dtRetMsgJAXBElement = (JAXBElement<com.tcl.sales.wsdl.fund.ZFMSDCRMFUNDResponse>)callClient.callWebService(url, requestDTO, marshaller);
com.sap.sales.wsdl.fund.ZFMSDCRMFUNDResponse dtRetMsg = dtRetMsgJAXBElement.getValue();
log.info(dtRetMsg.toString());
http basic认证的设置代码为
@Bean
HttpClient createHttpClient() {
List<Header> headers = new ArrayList<>();
BasicHeader authHeader = new BasicHeader("Authorization", "Basic " + authStr);
headers.add(authHeader);
// add more header as more as needed
RequestDefaultHeaders reqHeader = new RequestDefaultHeaders(headers);
CloseableHttpClient httpClient =
HttpClients.custom()
.addInterceptorFirst(new HttpComponentsMessageSender.RemoveSoapHeadersInterceptor())
.addInterceptorLast(reqHeader)
// .setProxy( new HttpHost("127.0.0.1", 8080, "http"))
.build();
return httpClient;
}
若调用出现问题,我通常的解决方案是设置http代理,使用burpsuit工具,查看http的请求和返回数据(因为ieda无法看到原始的http请求和响应数据)
参考:(69条消息) maven 生成webservice客户端代码_灰色轨迹-CSDN博客https://blog.csdn.net/lorry2010/article/details/84830202