Java 将 SoapHeader 添加到 org.springframework.ws.WebServiceMessage
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2274378/
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
Add SoapHeader to org.springframework.ws.WebServiceMessage
提问by Mark Pope
How can I add an object into the soap header of a org.springframework.ws.WebServiceMessage
如何将对象添加到 a 的soap 标头中 org.springframework.ws.WebServiceMessage
This is the structure I'm looking to end up with:
这是我希望最终得到的结构:
<soap:Header>
<credentials xmlns="http://example.com/auth">
<username>username</username>
<password>password</password>
</credentials>
</soap:Header>
采纳答案by Pascal Thivent
Basically, you need to use a WebServiceMessageCallback
in your client to modify the message after its creation but before it is sent. To rest of the code has been described pretty accurately by @skaffman so the whole stuff might look like this:
基本上,您需要WebServiceMessageCallback
在客户端中使用 a在消息创建之后但在发送之前修改消息。@skaffman 已经非常准确地描述了其余代码,因此整个内容可能如下所示:
public void marshalWithSoapActionHeader(MyObject o) {
webServiceTemplate.marshalSendAndReceive(o, new WebServiceMessageCallback() {
public void doWithMessage(WebServiceMessage message) {
try {
SoapMessage soapMessage = (SoapMessage)message;
SoapHeader header = soapMessage.getSoapHeader();
StringSource headerSource = new StringSource("<credentials xmlns=\"http://example.com/auth\">\n +
<username>"+username+"</username>\n +
<password>"+password"+</password>\n +
</credentials>");
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(headerSource, header.getResult());
} catch (Exception e) {
// exception handling
}
}
});
}
Personally, I find that Spring-WS sucks hard for such a basic need, they should fix SWS-479.
就个人而言,我发现 Spring-WS 很难满足这样的基本需求,他们应该修复 SWS-479。
回答by skaffman
You need to cast the WebServiceMessage
to SoapMessage
, which has a getSoapHeader()
method you can use to modify the header. In turn, SoapHeader
has various methods for adding elements, including getResult()
(which can be used as the output of a Transformer.transform()
operation).
您需要强制转换WebServiceMessage
to SoapMessage
,它具有getSoapHeader()
可用于修改标题的方法。反过来,SoapHeader
有各种添加元素的方法,包括getResult()
(可以用作Transformer.transform()
操作的输出)。
回答by pranav kumar
You can do as below:
您可以执行以下操作:
public class SoapRequestHeaderModifier implements WebServiceMessageCallback {
private final String userName = "user";
private final String passWd = "passwd";
@Override
public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
if (message instanceof SaajSoapMessage) {
SaajSoapMessage soapMessage = (SaajSoapMessage) message;
MimeHeaders mimeHeader = soapMessage.getSaajMessage().getMimeHeaders();
mimeHeader.setHeader("Authorization", getB64Auth(userName, passWd));
}
}
private String getB64Auth(String login, String pass) {
String source = login + ":" + pass;
String retunVal = "Basic " + Base64.getUrlEncoder().encodeToString(source.getBytes());
return retunVal;
}
}
Then
然后
Object response = getWebServiceTemplate().marshalSendAndReceive(request, new SoapRequestHeaderModifier());
回答by sundeep katiyar
Response response = (Response)getWebServiceTemplate() .marshalSendAndReceive(request, new HeaderModifier());
响应 response = (Response)getWebServiceTemplate() .marshalSendAndReceive(request, new HeaderModifier());
Create class HeaderModifier and override doWithMessage public class HeaderModifier implements WebServiceMessageCallback {
创建类 HeaderModifier 并覆盖 doWithMessage 公共类 HeaderModifier 实现 WebServiceMessageCallback {
private static PrintStream out = System.out;
@Override
public void doWithMessage(WebServiceMessage message) throws IOException {
SaajSoapMessage soapMessage = (SaajSoapMessage) message;
SoapEnvelope soapEnvelope = soapMessage.getEnvelope();
SoapHeader soapHeader = soapEnvelope.getHeader();
//Initialize QName for Action and To
QName action = new QName("{uri}","Action","{actionname}");
QName to = new QName("{uri}","To","{actionname}");
soapHeader.addNamespaceDeclaration("{actionname}", "{uri}");
SoapHeaderElement soapHeaderElementAction = soapHeader.addHeaderElement(action);
SoapHeaderElement soapHeaderElementTo = soapHeader.addHeaderElement(to);
soapHeaderElementAction.setText("{text inside the tags}");
soapHeaderElementTo.setText("{text inside the tags}");
soapMessage.setSoapAction("{add soap action uri}");
soapMessage.writeTo(out);
}
}
}