使用 Java 添加 Soap Action Header
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29653718/
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
Adding Soap Action Header using Java
提问by james2611nov
How do I add soap action header in java. I tested the service in SoapUI using <a:Action s:mustUnderstand="1">MyServiceName</a:Action>
in Header and it works fine as per this post SOAP Action mismatch error while testing a WCF service with SoapUI. Without this header I get The SOAP action specified on the message, '', does not match the HTTP SOAP Action,
error which is the same error I get from my Java client application.
如何在java中添加soap动作标头。我<a:Action s:mustUnderstand="1">MyServiceName</a:Action>
在 Header中使用在 SoapUI 中测试了该服务,并且在使用 SoapUI测试 WCF 服务时,它根据这篇文章SOAP Action mismatch error工作正常。如果没有这个标头,我会收到The SOAP action specified on the message, '', does not match the HTTP SOAP Action,
错误,这与我从 Java 客户端应用程序中得到的错误相同。
PS: I used Apache CXF to generate the stubs from the wsdl. I also tried using JAX-WS RI by using wsimport to generate the java client stubs. Same error using both the cases.
PS:我使用 Apache CXF 从 wsdl 生成存根。我还尝试通过使用 wsimport 生成 java 客户端存根来使用 JAX-WS RI。使用这两种情况相同的错误。
Any thoughts? I couldn't find a right conclusive post that address this issue in Java on SO.
有什么想法吗?我在 SO 上的 Java 中找不到解决此问题的正确结论性帖子。
Here is what I tried but I guess using classes from com.sun... package is not recommended and could cause portability issues across different jdks.JAX-WS - Adding SOAP Headers
这是我尝试过的,但我想使用来自 com.sun... 的类是不推荐的,并且可能会导致跨不同 jdks 的可移植性问题。JAX-WS - 添加 SOAP 标头
回答by Bharath V N
I was facing similar issue and here is what worked for me. I had generated the sei using wsimport.
我遇到了类似的问题,这对我有用。我已经使用 wsimport 生成了 sei。
If the headers are part of the wsdl, you can generate the SEI that accept the headers using -XadditionalHeaders.
如果头是 wsdl 的一部分,您可以使用 -XadditionalHeaders 生成接受头的 SEI。
If they are not, you will have to add the header programmatically using SOAPHandler. It is simple though!
如果不是,则必须使用 SOAPHandler 以编程方式添加标头。虽然很简单!
Here is a link with detailed description. http://www.javadb.com/using-a-message-handler-to-alter-the-soap-header-in-a-web-service-client/
这是一个包含详细说明的链接。 http://www.javadb.com/using-a-message-handler-to-alter-the-soap-header-in-a-web-service-client/
Change the method, handleMessageas below
更改方法,handleMessage如下
public boolean handleMessage(SOAPMessageContext smc) {
Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outboundProperty.booleanValue()) {
SOAPMessage message = smc.getMessage();
try {
SOAPFactory soapFactory = SOAPFactory.newInstance();
SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
SOAPHeader header = envelope.addHeader();
SOAPHeaderElement se=header.addHeaderElement(new QName("http://schemas.microsoft.com/ws/2005/05/addressing/none", "Action"));
//se.setMustUnderstand(true); //Ideal way to set if webservice supports
se.addTextNode("some text");
se.addAttribute(soapFactory.createName("S:mustUnderstand"),"1"); //S: or s: depending on xmlns
} catch (Exception e) {
e.printStackTrace();
}
} else {
try {
SOAPMessage message = smc.getMessage();
message.writeTo(System.out);
System.out.println("");
} catch (Exception ex) {
ex.printStackTrace();
}
}
return true;
}
//Code to attach handler.
// 附加处理程序的代码。
Service1 service1 = new Service1();
IService1 iService1 = service1.getBasicHttpBindingIService1();
BindingProvider bindingProvider = (BindingProvider) iService1;
final Binding binding = bindingProvider.getBinding();
List<Handler> handlerList = binding.getHandlerChain();
if (handlerList == null) {
handlerList = new ArrayList<Handler>();
}
handlerList.add(new HeaderHandler());
binding.setHandlerChain(handlerList);
ServiceResponse serviceResponse = iService1.callServiceMethod1(serviceRequest);