java 添加soapheader,使用Java添加Soap Action Header

How do I add soap action header in java. I tested the service in SoapUI using MyServiceName 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.

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.

Any thoughts? I couldn't find a right conclusive post that address this issue in Java on SO.

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

解决方案

I was facing similar issue and here is what worked for me. I had generated the sei using wsimport.

If the headers are part of the wsdl, you can generate the SEI that accept the headers using -XadditionalHeaders.

If they are not, you will have to add the header programmatically using SOAPHandler. It is simple though!

Change the method, handleMessage as below

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 handlerList = binding.getHandlerChain();

if (handlerList == null) {

handlerList = new ArrayList();

}

handlerList.add(new HeaderHandler());

binding.setHandlerChain(handlerList);

ServiceResponse serviceResponse = iService1.callServiceMethod1(serviceRequest);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值