万一有人偶然发现了类似的问题,我自己想通了。我能够通过 CamelContext 检索 CxfEndpoint:
camelContext.getEndpoint(endpointUrl, CxfEndpoint.class);
然后我可以添加我创建的拦截器:
public class MyCxfInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
...
使用 CxfEndpoint 方法:
cxfEndpoint.getOutInterceptors().add(new MyCxfInterceptor());
在我的拦截器中,我还合并了另一个拦截器 SAAJOutInterceptor,它将 SOAP 转换为易于使用的对象:
private List<PhaseInterceptor<? extends Message>> extras = new ArrayList<>(1);
public MyCxfInterceptor() {
super(Phase.USER_PROTOCOL);
extras.add(new SAAJOutInterceptor());
}
public Collection<PhaseInterceptor<? extends Message>> getAdditionalInterceptors() {
return extras;
}
易于使用的 SOAP 消息:
@Override
public void handleMessage(SoapMessage soapMessage) throws Fault {
SOAPMessage msg = soapMessage.getContent(SOAPMessage.class);
try {
SOAPBody soapBody = msg.getSOAPBody();
然后使用 XPATH 对传出的 SOAP 消息进行更正就很简单了。
private XPath xpath = XPathFactory.newInstance().newXPath();
...
NodeList nodeList = soapBody.getElementsByTagName("tagName");
for (int x = 0; x < nodeList.getLength(); x++) {
Node node = nodeList.item(x);
((Element) node).setAttribute("missingAttributeName", "missingAttributeValue");
}
我希望这可以帮助任何使用具有挑战性的 SOAP 服务的人!
感谢博客,它在使我能够实施此解决方案方面发挥了重要作用:https ://xceptionale.wordpress.com/2016/06/26/message-interceptor-to-modify-outbound-soap-request/