SpringBoot + cxf 调用 webService

简介: SpringBoot + cxf 调用 webService

1、添加cxf依赖

<dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-spring-boot-starter-jaxws</artifactId><version>3.2.4</version></dependency>

2、代码

importorg.apache.cxf.endpoint.Client;
importorg.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
importjavax.xml.namespace.QName;
/*** @author mjx* @date 2021年10月19日 9:03*/publicclassWebServiceHttp {
/*** @param wsdlUrl: webService 路径 :http://ip:port/web/services/**?wsdl* @param nameSpaceUrl: 命名空间地址:http://server.webservice.core.epm*        methedName: 调用方法名称:invoke*        sendMessage: 请求报文 xml* @return* @author: mjx* @createtime: 2021/11/22 10:22*/publicstaticvoidsendMessage(StringwsdlUrl,StringnameSpaceUrl,StringmethedName,StringsendMessage) {
JaxWsDynamicClientFactoryjaxWsDynamicClientFactory=JaxWsDynamicClientFactory.newInstance();
Stringpath="bemgnt/real/service/RealNameService";
StringmethodName="saveCertCheck";
Clientclient=jaxWsDynamicClientFactory.createClient(wsdlUrl);
// 请求头信息client.getOutInterceptors().add(newWebServiceClientAuth());
QNameqName=newQName(nameSpaceUrl, methedName);
Object[] invoke=newObject[0];
try {
System.out.println("invoke 调用开始========================");
// 多个 参数传递invoke=client.invoke(qName,path,methodName,sendMessage);
        } catch (Exceptione) {
System.out.println("调用webService异常"+e.getMessage());
e.printStackTrace();
        }
System.out.println("调用同步返回 "+String.valueOf(invoke[0]));
System.out.println("invoke 调用结束========================");
    }
}

设置请求头部分(如果有)

importorg.apache.cxf.binding.soap.SoapMessage;
importorg.apache.cxf.headers.Header;
importorg.apache.cxf.helpers.DOMUtils;
importorg.apache.cxf.interceptor.Fault;
importorg.apache.cxf.phase.AbstractPhaseInterceptor;
importorg.apache.cxf.phase.Phase;
importorg.dom4j.DocumentException;
importorg.dom4j.DocumentHelper;
importorg.w3c.dom.Document;
importorg.w3c.dom.Element;
importjavax.xml.namespace.QName;
importjavax.xml.parsers.DocumentBuilder;
importjava.util.List;
/*** @author mjx* @date 2021年11月19日 11:36*/publicclassWebServiceClientAuthextendsAbstractPhaseInterceptor<SoapMessage> {
publicWebServiceClientAuth() {
//准备发送阶段super(Phase.PREPARE_SEND);
    }
/**** <soapenv:Header>*     <ns1:username xmlns:ns1='**'>zz</ns1:username>*     <ns2:password xmlns:ns2='**'>1</ns2:password>*  </soapenv:Header>*** @author: MJX* @createtime: 2021/11/22 10:26*/@OverridepublicvoidhandleMessage(SoapMessagemessage) throwsFault {
List<Header>headers=message.getHeaders();
Documentdocument=DOMUtils.createDocument();
Elementname=document.createElement("ns1:username");
name.setAttribute("xmlns:ns1","**");
name.setTextContent("zz");
Elementpassword=document.createElement("ns2:password");
password.setAttribute("xmlns:ns2","**");
password.setTextContent("1");
headers.add(newHeader(newQName(""),name));
headers.add(newHeader(newQName(""),password));
    }
}


目录
相关文章
|
2月前
【Azure 应用服务】Web App Service 中的 应用程序配置(Application Setting) 怎么获取key vault中的值
【Azure 应用服务】Web App Service 中的 应用程序配置(Application Setting) 怎么获取key vault中的值
|
2月前
|
关系型数据库 MySQL Linux
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
|
2月前
|
Shell PHP Windows
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
|
2月前
|
Linux 应用服务中间件 网络安全
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?
|
2月前
【Azure 应用服务】通过 Web.config 开启 dotnet 应用的 stdoutLog 日志,查看App Service 产生500错误的原因
【Azure 应用服务】通过 Web.config 开启 dotnet 应用的 stdoutLog 日志,查看App Service 产生500错误的原因
|
2月前
|
Linux Python
【Azure 应用服务】Azure App Service For Linux 上实现 Python Flask Web Socket 项目 Http/Https
【Azure 应用服务】Azure App Service For Linux 上实现 Python Flask Web Socket 项目 Http/Https
|
2月前
|
存储 安全 网络安全
【Azure 环境】使用Azure中的App Service部署Web应用,以Windows为主机系统是否可以启动防病毒,防恶意软件服务呢(Microsoft Antimalware)?
【Azure 环境】使用Azure中的App Service部署Web应用,以Windows为主机系统是否可以启动防病毒,防恶意软件服务呢(Microsoft Antimalware)?
|
2月前
|
存储 Linux 网络安全
【Azure 应用服务】App Service For Linux 如何在 Web 应用实例上住抓取网络日志
【Azure 应用服务】App Service For Linux 如何在 Web 应用实例上住抓取网络日志
|
2月前
【Azure 云服务】Azure Cloud Service 为 Web Role(IIS Host)增加自定义字段 (把HTTP Request Header中的User-Agent字段增加到IIS输出日志中)
【Azure 云服务】Azure Cloud Service 为 Web Role(IIS Host)增加自定义字段 (把HTTP Request Header中的User-Agent字段增加到IIS输出日志中)
|
2月前
|
Web App开发 安全 JavaScript
【Azure 应用服务】App Service 通过配置web.config来添加请求返回的响应头(Response Header)
【Azure 应用服务】App Service 通过配置web.config来添加请求返回的响应头(Response Header)
下一篇
无影云桌面