使用Java添加Soap Action Header [英] Adding Soap Action Header using Java

查看:187
本文介绍了使用Java添加Soap Action Header的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Java中添加肥皂动作标头。我在Header中使用< a:Action s:mustUnderstand = 1> MyServiceName< / a:Action> 在SoapUI中测试了该服务,按此方法可以正常工作发表 SOAP使用SoapUI测试WCF服务时出现操作不匹配错误。没有此标头,我将得到消息上指定的SOAP操作与HTTP SOAP Action不匹配,错误与我从Java客户端应用程序得到的错误相同

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:我使用Apache CXF从wsdl生成存根。我还尝试通过使用wsimport来生成Java客户端存根来使用JAX-WS RI。两种情况下都存在相同的错误。

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.

有什么想法吗?我找不到合适的结论性文章来解决Java on SO上的这个问题。

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

这是我尝试过的方法,但我想使用com.sun中的类...不建议使用此软件包,它可能会导致跨不同jdk的可移植性问题。 JAX-WS-添加SOAP标头

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

推荐答案

我遇到了类似的问题,这对我有用。我已经使用wsimport生成了sei。

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

如果标头是wsdl的一部分,则可以使用-XadditionalHeaders生成可以接受标头的SEI。

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

如果不是,则必须使用SOAPHandler以编程方式添加标头。

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

这里是带有详细说明的链接。
http://www.javadb.com/using-a-message-handler-to-alter-the-soap-header-in-a-web-service-client/

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/

更改方法,如下所示 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);

这篇关于使用Java添加Soap Action Header的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆