4

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.

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

Community
  • 1
  • 1
james2611nov
  • 473
  • 2
  • 10
  • 27

1 Answers1

4

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!

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/

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

        if (handlerList == null) {
            handlerList = new ArrayList<Handler>();
        }

        handlerList.add(new HeaderHandler());
        binding.setHandlerChain(handlerList);
        ServiceResponse serviceResponse = iService1.callServiceMethod1(serviceRequest);
  • Thanks but in the link it doesn't show where you actually call the handleMessage method? Also where do we get the SOAPMessageContext parameter to pass it to handleMessage from? – james2611nov Apr 17 '15 at 16:24
  • Thanks.This almost did the job for me. But I used @HandlerChain(file="handler-chain.xml") to link this above Handler to the Proxy generated service. More details here: http://www.mkyong.com/webservices/jax-ws/jax-ws-soap-handler-in-client-side/ – james2611nov Apr 21 '15 at 14:38
  • Sorry for the delayed response. Here is the code that I have written to call the method. – Bharath V N Apr 22 '15 at 08:43
  • "If the headers are part of the wsdl, you can generate the SEI that accept the headers using -XadditionalHeaders." ..... I would like to try that, adding the headers to the WSDL file, but have no clue how to do it. wsimport.exe put the soap action in the "Content-Type" http header, the the service says the Action header is missing in the SOAP message. The WSDL specifies the Action as an attribute of the element inside the section. If I could specifiy the Action as a "header" element in the header section, perhaps wsimport.exe would generate working code. – sb4 Oct 15 '20 at 18:33