面临以下问题:我有一个 wsdl 文件,用于通过 SOAP 通信我的服务和 CRM。请求的主体是通过从 wsdl 配置 java 文件来填充的。但是标题必须手动编写。我正在使用以下技术发送我的请求:
final Map<String, String> gateValues = new HashMap<>();
final String username = Objects.requireNonNull(config).getSoapconf().getUsername();
final String password = Objects.requireNonNull(config).getSoapconf().getPassword();
final String webServices = Objects.requireNonNull(config).getSoapconf().getSessionType();
gateValues.put("UsernameToken", username);
gateValues.put("PasswordText", password);
gateValues.put("SessionType", webServices);
GetInput rq = new GetInput();
rq.setMyParam(param);
log.info("Requesting {}", param);
try {
GetOutput client = (GetOutput) getWebServiceTemplate().marshalSendAndReceive(rq, (message) -> {
if (message instanceof SaajSoapMessage) {
SaajSoapMessage saajSoapMessage = (SaajSoapMessage) message;
SOAPMessage soapMessage = saajSoapMessage.getSaajMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
try {
SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
SOAPHeader soapHeader = soapEnvelope.getHeader();
gateValues.forEach((key, value) -> {
try {
Name headerElementName = soapEnvelope.createName(
key,
PREFIX,
NAMESPACE
);
System.out.println(headerElementName.getLocalName() + "\n" + headerElementName.getQualifiedName());
SOAPHeaderElement soapHeaderElement = soapHeader.addHeaderElement(headerElementName);
soapHeaderElement.setTextContent(value);
} catch (SOAPException e) {
// error handling
}
});
soapMessage.saveChanges();
soapMessage.writeTo(System.out);
System.out.println(soapEnvelope.getHeader());
} catch (SOAPException e) {
System.out.println();
}
}
});
return client;
} catch (Exception e) {
ExceptionUtils.getStackTrace(e);
}
已添加最后一个捕获以查找错误。从 WebServiceTemplate 类的 doSendAndReceive 方法中丢弃错误 org.springframework.ws.soap.client.SoapFaultClientException。
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header>
<PasswordText xmlns="http://***.com/services">myPass</PasswordText>
<SessionType xmlns="http://****.com/services">mySessionType</SessionType>
<UsernameToken xmlns="http://***.com/services">myUsName</UsernameToken>
</SOAP-ENV:Header>
<SOAP-ENV:Body><ns2:Get_Input xmlns:ns2="http://***.com/UI" xmlns:ns3="http://***.com/xml/%20External">
<ns2:param>238068014416</ns2:param>
</ns2:Get_Input>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
什么不见了?为什么 SOAPUI - 它是有效的。客户端请求是按照Add SoapHeader to org.springframework.ws.WebServiceMessage的最后一个答案的示例进行的