java HTTP 标头中缺少 Spring WebServiceTemplate SOAPAction

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11631833/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:52:18  来源:igfitidea点击:

Spring WebServiceTemplate SOAPAction missing in HTTP Header

javaspringsoapspring-ws

提问by user1546703

I am having difficulty in calling a SOAP 1.2 WebService via Spring-ws WebServiceTemplate. The request being made is missing SOAPAction in Http Header and the server throws an error with "Unable to handle request without a valid action parameter. Please supply a valid soap action." I was able to figure out SOAP Action was missing by monitoring through wireshark. I am also not behind any proxy.

我在通过 Spring-ws WebServiceTemplate 调用 SOAP 1.2 WebService 时遇到了困难。发出的请求在 Http 标头中缺少 SOAPAction,并且服务器抛出错误“无法处理没有有效动作参数的请求。请提供有效的肥皂动作”。通过wireshark进行监控,我能够确定缺少SOAP Action。我也不支持任何代理。

I have made sure that the SOAP XML I am trying to send is valid by running the request through TCP Mon ( tool like SOAP UI) and was able to get response.

我已经通过 TCP Mon(类似于 SOAP UI 的工具)运行请求来确保我尝试发送的 SOAP XML 是有效的,并且能够获得响应。

Here is my spring config:

这是我的弹簧配置:

<?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                        http://www.springframework.org/schema/util
                        http://www.springframework.org/schema/util/spring-util-3.0.xsd">

<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
    <property name="soapVersion">
    <util:constant static-field="org.springframework.ws.soap.SoapVersion.SOAP_12" />
    </property>
</bean>

<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
 <constructor-arg ref="messageFactory" />
<property name="defaultUri" value="https://ecomapi.networksolutions.com/soapservice.asmx" />
<property name="messageSender">
    <bean class="org.springframework.ws.transport.http.CommonsHttpMessageSender" />     </property>
</bean>

And this is my java code:

这是我的java代码:

            public void simpleSendAndReceive() {
            try{
            StreamSource source = new StreamSource(new StringReader(MESSAGE));
            StreamResult result = new StreamResult(System.out);
            SoapActionCallback actionCallBack = new SoapActionCallback("https://ecomapi.networksolutions.com/soapservice.asmx") {
                public void doWithMessage(WebServiceMessage msg) {
                    SoapMessage smsg = (SoapMessage)msg;
                    smsg.setSoapAction("http://networksolutions.com/ReadOrder");
                }
            };
            webServiceTemplate.sendSourceAndReceiveToResult(
                    "https://ecomapi.networksolutions.com/soapservice.asmx",
                     source,
                     new SoapActionCallback("http://networksolutions.com/ReadOrder"),
     //                      actionCallBack,
                     result);


            System.out.println(source.getInputStream().toString());
            System.out.println(result.getWriter().toString());

            }catch (SoapFaultClientException e) {
                System.out.println(e.getFaultCode());
                System.out.println(e.getFaultStringOrReason());
                System.out.println(e.fillInStackTrace().getLocalizedMessage());
            } catch (WebServiceIOException we) {
                System.out.println(we.getRootCause());
            }
        }

回答by John Shepherd

I faced the same problem and I traced it back to a bug in the 1.3.2 version of the com.sun.xml.messaging.saaj.soap.MessageImpl implementation (http://java.net/jira/browse/SAAJ-37). I upgraded to the 1.3.19 version and all was fine.

我遇到了同样的问题,我将其追溯到 1.3.2 版本的 com.sun.xml.messaging.saaj.soap.MessageImpl 实现(http://java.net/jira/browse/SAAJ- 37)。我升级到 1.3.19 版本,一切正常。

The Issue:-

问题:-

Before writing the request to the output stream, the saveChanges() method is called. The 1.3.2 version of this class was overwriting the 'Content-Type' request header in it's implementation of the 'saveChanges' method.

在将请求写入输出流之前,会调用 saveChanges() 方法。此类的 1.3.2 版本在其“saveChanges”方法的实现中覆盖了“Content-Type”请求标头。

回答by Frequentcrasher

I had a similar issue which was due to the namespace of the XmlSchema annotation in the package-info.java file being different from what the soap service I was trying to call expected. This resulted from me using xjc to generate the jaxb request and response objects from a xsd that I had sewn together from the wsdl parts plus the xsd header from a previous service I was interfacing so it was a pure cut and paste error on my part but the previous answer on here about the namespace was what clued me in to my error quickly.

我有一个类似的问题,这是由于 package-info.java 文件中 XmlSchema 注释的命名空间与我试图调用的soap服务不同。这是因为我使用 xjc 从一个 xsd 生成 jaxb 请求和响应对象,我从 wsdl 部分加上我正在连接的以前服务的 xsd 标头缝合在一起,所以这对我来说是一个纯粹的剪切和粘贴错误,但是上一个关于命名空间的答案让我很快发现了我的错误。

回答by Amio.io

SOAPAction is mandatory only in SOAP 1.1, see section Content Type.

SOAPAction 仅在 SOAP 1.1 中是强制性的,请参阅部分内容类型

The SOAP 1.1 mandatory SOAPAction HTTP header has been removed in SOAP 1.2. In its place is an optional action parameter on the application/soap+xml media type.

SOAP 1.1 强制性 SOAPAction HTTP 标头已在 SOAP 1.2 中删除。取而代之的是 application/soap+xml 媒体类型上的可选操作参数。