java 如何修改生成的 SOAP 请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6906499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:51:24  来源:igfitidea点击:

How to modify the generated SOAP request?

javacxfinterceptoroutputstream

提问by kiwifrog

I'm at the stage where I created an output interceptor and I get an OuputStream out of the SOAP message. But how could I modify the SOAP envelope right before sending it to the endpoint? I would like to delete some xml elements.

我正处于创建输出拦截器的阶段,并从 SOAP 消息中获得一个 OuputStream。但是如何在将 SOAP 信封发送到端点之前修改它呢?我想删除一些 xml 元素。

采纳答案by Newtopian

one way could be to get the document and run it through XSLT transform.

一种方法是获取文档并通过 XSLT 转换运行它。

You can get at the document in the handleMessage of your interceptor by calling

您可以通过调用获取拦截器的 handleMessage 中的文档

@Override
public void handleMessage(SoapMessage message) throws Fault{
    SOAPMessage saaj = message.getContent(SOAPMessage.class);
    Document doc = saaj.getSOAPPart(); // This actually returns a SOAPPart instance but it does implement the w3c Document interface

    //play around with the document, doc is a reference so any changes made to that instance
    //will be forwarded to the rest of the chain
}

careful though that if you have security such as XML signature that must be performed on the soap content you must ensure that your interceptor occurs BEFORE the signature are applied otherwise you will invalidate them.

但是请注意,如果您具有必须对soap内容执行的XML签名等安全性,您必须确保在应用签名之前发生拦截器,否则您将使它们无效。

To play around with the timing of the interceptor you can specify the phase at which it will run. CXF should also honor the order in which you will configure them should they be performed at the same phase.

要调整拦截器的时间,您可以指定它将运行的阶段。如果它们在同一阶段执行,CXF 还应遵守您配置它们的顺序。

but don't take my word for it... check these for more info

但不要相信我的话......查看这些以获取更多信息

debugging through the CXF source code also helped me a great deal in understanding how it worked

通过 CXF 源代码进行调试也对我了解它的工作原理有很大帮助

---- EDIT ----

- - 编辑 - -

(thanks Daniel :-)

(感谢丹尼尔 :-)

For this to work you need to have SAAJOutInterceptor configured in your stack. You can either add it manually or simply make it part of your interceptor. Here isan example of an interceptor that pretty much does what you want.

为此,您需要在堆栈中配置 SAAJOutInterceptor。您可以手动添加它,也可以简单地将其作为拦截器的一部分。 这是一个拦截器的示例,它几乎可以满足您的要求。

回答by snowindy

I posted an answer here https://stackoverflow.com/a/12948702/792313It is based on the whole body substitution.

我在这里发布了一个答案https://stackoverflow.com/a/12948702/792313它基于全身替换。