C# 即使 SOAP 请求发送正确,Axis2 也总是接收空参数?

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

Axis2 always receives null parameters even if SOAP request is sent correctly?

c#javaweb-servicessoapaxis2

提问by HalliHax

UPDATE: I HAVE SOLVED THIS PROBLEM NOW - PLEASE SCROLL TO BOTTOM FOR INFORMATION ABOUT FIX

更新:我现在已经解决了这个问题 - 请滚动到底部以获取有关 FIX 的信息

Hi guys,

嗨,大家好,

I have a web-service written in Java, hosted on an Axis2 / Tomcat / Apache server. My client software is written in C#.

我有一个用 Java 编写的 Web 服务,托管在 Axis2/Tomcat/Apache 服务器上。我的客户端软件是用 C# 编写的。

I have had a few irritating problems with the way java2wsdl generates the wsdl file, which did cause me a few headaches early on, but with this problem I am completely stumped.

我在 java2wsdl 生成 wsdl 文件的方式上遇到了一些恼人的问题,这在早期确实让我有些头疼,但是对于这个问题,我完全被难住了。

Basically what is happening is that the client sees the web service fine, and sends a perfectly valid (or at least, it looks valid to me) SOAP request with parameters.

基本上发生的事情是客户端看到 Web 服务很好,并发送一个完全有效(或者至少,它对我来说看起来有效)带有参数的 SOAP 请求。

On the server, the correct web method is executed, but the parameters are all null. My web service detects this and builds up a response, which the client receives and understands perfectly well.

在服务器端,执行了正确的web方法,但是参数都是空的。我的网络服务检测到这一点并建立一个响应,客户端接收并完全理解。

My hunch is that Axis2 is falling flat on its face somewhere, but given the headaches I have had with java2wsdl, perhaps all I need is a change in my wsdl file.

我的预感是 Axis2 在某个地方失败了,但是考虑到我对 java2wsdl 的头痛,也许我需要的只是更改我的 wsdl 文件。

Here is the wsdl file:

这是 wsdl 文件:

    <?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:axis2="http://stws/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:ns0="http://stws/xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://stws/">
    <wsdl:types>
        <xs:schema xmlns:ns="http://stws/xsd" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://stws/xsd">
            <xs:element name="GetGroups">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="serialcode" nillable="true" type="xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="GetGroupsResponse">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element maxOccurs="unbounded" minOccurs="0" name="return" nillable="true" type="ns0:Group"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:complexType name="Group">
                <xs:sequence>
                    <xs:element minOccurs="0" name="ID" type="xs:int"/>
                    <xs:element minOccurs="0" name="Name" nillable="true" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
            <xs:element name="GetMessages">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="serialcode" nillable="true" type="xs:string"/>
                        <xs:element maxOccurs="unbounded" minOccurs="0" name="groupids" type="xs:int"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="GetMessagesResponse">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element maxOccurs="unbounded" minOccurs="0" name="return" nillable="true" type="ns0:Message"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:complexType name="Message">
                <xs:sequence>
                    <xs:element minOccurs="0" name="date" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="group" type="xs:int"/>
                    <xs:element minOccurs="0" name="messageID" type="xs:int"/>
                    <xs:element minOccurs="0" name="text" nillable="true" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
        </xs:schema>
    </wsdl:types>
    <wsdl:message name="GetMessagesRequest">
        <wsdl:part name="parameters" element="ns0:GetMessages"/>
    </wsdl:message>
    <wsdl:message name="GetMessagesResponse">
        <wsdl:part name="parameters" element="ns0:GetMessagesResponse"/>
    </wsdl:message>
    <wsdl:message name="GetGroupsRequest">
        <wsdl:part name="parameters" element="ns0:GetGroups"/>
    </wsdl:message>
    <wsdl:message name="GetGroupsResponse">
        <wsdl:part name="parameters" element="ns0:GetGroupsResponse"/>
    </wsdl:message>
    <wsdl:portType name="MyProjectPortType">
        <wsdl:operation name="GetMessages">
            <wsdl:input message="axis2:GetMessagesRequest" wsaw:Action="urn:GetMessages"/>
            <wsdl:output message="axis2:GetMessagesResponse" wsaw:Action="urn:GetMessagesResponse"/>
        </wsdl:operation>
        <wsdl:operation name="GetGroups">
            <wsdl:input message="axis2:GetGroupsRequest" wsaw:Action="urn:GetGroups"/>
            <wsdl:output message="axis2:GetGroupsResponse" wsaw:Action="urn:GetGroupsResponse"/>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="MyProjectSOAP11Binding" type="axis2:MyProjectPortType">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <wsdl:operation name="GetMessages">
            <soap:operation soapAction="urn:GetMessages" style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="GetGroups">
            <soap:operation soapAction="urn:GetGroups" style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:binding name="MyProjectSOAP12Binding" type="axis2:MyProjectPortType">
        <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <wsdl:operation name="GetMessages">
            <soap12:operation soapAction="urn:GetMessages" style="document"/>
            <wsdl:input>
                <soap12:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap12:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="GetGroups">
            <soap12:operation soapAction="urn:GetGroups" style="document"/>
            <wsdl:input>
                <soap12:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap12:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:binding name="MyProjectHttpBinding" type="axis2:MyProjectPortType">
        <http:binding verb="POST"/>
        <wsdl:operation name="GetMessages">
            <http:operation location="MyProject/GetMessages"/>
            <wsdl:input>
                <mime:content type="text/xml" part="GetMessages"/>
            </wsdl:input>
            <wsdl:output>
                <mime:content type="text/xml" part="GetMessages"/>
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="GetGroups">
            <http:operation location="MyProject/GetGroups"/>
            <wsdl:input>
                <mime:content type="text/xml" part="GetGroups"/>
            </wsdl:input>
            <wsdl:output>
                <mime:content type="text/xml" part="GetGroups"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="MyProject">
        <wsdl:port name="MyProjectSOAP11port_http" binding="axis2:MyProjectSOAP11Binding">
            <soap:address location="http://localhost:8080/axis2/services/MyProject"/>
        </wsdl:port>
        <wsdl:port name="MyProjectSOAP12port_http" binding="axis2:MyProjectSOAP12Binding">
            <soap12:address location="http://localhost:8080/axis2/services/MyProject"/>
        </wsdl:port>
        <wsdl:port name="MyProjectHttpport" binding="axis2:MyProjectHttpBinding">
            <http:address location="http://localhost:8080/axis2/services/MyProject"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

And here is a sample request and response:

这是一个示例请求和响应:

Request:

要求:

<?xml version='1.0' encoding='utf-8'?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soap:Body>
    <GetGroups xmlns="http://stws/xsd">
      <serialcode>123456-654321</serialcode>
    </GetGroups>
  </soap:Body>
</soap:Envelope>

Response

回复

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
    <GetGroupsResponse xmlns="http://stws/xsd">
      <return>
        <ID>-101</ID>
        <name>ERROR: Empty Serial</name>
      </return>
    </GetGroupsResponse>
  </soapenv:Body>
</soapenv:Envelope>

Does anybody have any idea what could be going wrong?

有谁知道可能会出什么问题?

The error message in the response can only be sent when the serialcode parameter in the request is empty / null, so I'm guessing there is something wrong with how Axis2 is reading my parameters.

响应中的错误消息只能在请求中的 serialcode 参数为空/空时发送,所以我猜测 Axis2 读取我的参数的方式有问题。

Any help is much appreciated.

任何帮助深表感谢。

============================================================

================================================== ==========

HOW TO FIX THIS:

如何解决这个问题:

This is in response to Aldo's request for more information about how I fixed this problem.

这是为了响应 Aldo 要求提供有关我如何解决此问题的更多信息的请求。

I am unsure of why this fix works - perhaps it is just a bug in Axis2 or something. Either way, YMMV as I don't know whether the problem was caused by my setup or something else. All I can say is that by doing the following, everything started working.

我不确定为什么此修复程序有效 - 也许它只是 Axis2 中的错误或其他什么。无论哪种方式,YMMV,因为我不知道问题是由我的设置还是其他原因引起的。我只能说,通过执行以下操作,一切都开始了。

Anyway, the auto-generated WSDL file creates complex-element types for web requests and their parameters, even when the only parameters are simple types such as strings or integers. What I did was go through and create the correct simple-type tags for parameters (such as 'serialcode' or 'date-string'), then replace the references to the complex types elsewhere in the WSDL file with references to the simple types.

无论如何,自动生成的 WSDL 文件为 Web 请求及其参数创建复杂元素类型,即使只有参数是简单类型(如字符串或整数)。我所做的是为参数创建正确的简单类型标签(例如“serialcode”或“date-string”),然后用对简单类型的引用替换 WSDL 文件中其他地方对复杂类型的引用。

An example is below:

一个例子如下:

Auto Generated WSDL method and parameters

自动生成的 WSDL 方法和参数

<!--Requests-->    
<wsdl:message name="RegisterClientRequest">
    <wsdl:part name="parameters" element="ns0:RegisterClient"/>
</wsdl:message>
<wsdl:message name="GetGroupsRequest">
    <wsdl:part name="parameters" element="ns0:GetGroups"/>
</wsdl:message>

<!--Parameters-->
<xs:element name="RegisterClient">
    <xs:complexType>
        <xs:sequence>
           <xs:element minOccurs="0" name="serialcode" nillable="true" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:element name="GetGroups">
    <xs:complexType>
        <xs:sequence>
            <xs:element minOccurs="0" name="serialcode" nillable="true" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
 </xs:element>

Basically what you should do is discard the auto-generated parameters and create simple-types. You then modify the 'request' tags to use 'type' rather than 'element', and use your newly created simple-types.

基本上你应该做的是丢弃自动生成的参数并创建简单类型。然后修改“请求”标签以使用“类型”而不是“元素”,并使用新创建的简单类型。

Modified / Fixed WSDL

修改/固定 WSDL

<!--Requests-->    
<wsdl:message name="RegisterClientRequest">
    <wsdl:part name="parameters" type="ns0:SerialCode"/>
</wsdl:message>
<wsdl:message name="GetGroupsRequest">
    <wsdl:part name="parameters" type="ns0:SerialCode"/>
</wsdl:message>

<!--Parameters-->
<xs:simpleType name="SerialCode">
    <xs:restriction base="xs:string"/>
</xs:simpleType>

Obviously it depends on what your parameters actually are. In my case they are all standard simple types such as strings and integers. If you are passing more than one parameter, you may need to play around by retaining the auto-generated elements but making sure that the element refers to simple types rather than just including the type attribute as 'xs:string' or something of that nature.

显然,这取决于您的实际参数是什么。就我而言,它们都是标准的简单类型,例如字符串和整数。如果传递多个参数,则可能需要保留自动生成的元素,但要确保该元素引用简单类型,而不是仅将 type 属性包含为“xs:string”或类似性质的内容.

Apologies I can't be more clear on this, but as I said earlier - I don't know why this works.

抱歉,我对此不能说得更清楚,但正如我之前所说 - 我不知道为什么会这样。

One final thing: By removing the 'element' reference attribute in the request tags - you may receive a parser warning in your Axis2 logs. So far this has not caused me any problems, but it's something to be aware of in case you run into trouble.

最后一件事:通过删除请求标记中的“元素”引用属性 - 您可能会在 Axis2 日志中收到解析器警告。到目前为止,这还没有给我带来任何问题,但如果您遇到麻烦,请注意这一点。

采纳答案by HalliHax

I solved this issue by going through my WSDL file and, wherever possible, breaking down elements into their simple type counterparts and updating the references between the XML elements accordingly.

我通过检查我的 WSDL 文件解决了这个问题,并尽可能将元素分解为它们的简单类型对应物,并相应地更新 XML 元素之间的引用。

I am unsure why this works, but it has solved my problem anyway.

我不确定为什么会这样,但无论如何它已经解决了我的问题。

回答by Tim Reddy

This is just a hunch, but maybe you're having a namespace issue. If you focus on this part of the wsdl, notice that your parameter has an "ns0" namespace for the elements, but in your operations defined later, it looks like you're using an "axis2" namespace. With all of my Axis2 generated WSDLs, these two namespaces are the same.

这只是一种预感,但也许您遇到了命名空间问题。如果您关注 wsdl 的这一部分,请注意您的参数具有用于元素的“ns0”命名空间,但在稍后定义的操作中,看起来您使用的是“axis2”命名空间。对于我所有的 Axis2 生成的 WSDL,这两个名称空间是相同的。

<wsdl:message name="GetMessagesRequest">
    <wsdl:part name="parameters" element="ns0:GetMessages"/>
</wsdl:message>
<wsdl:message name="GetMessagesResponse">
    <wsdl:part name="parameters" element="ns0:GetMessagesResponse"/>
</wsdl:message>
<wsdl:message name="GetGroupsRequest">
    <wsdl:part name="parameters" element="ns0:GetGroups"/>
</wsdl:message>
<wsdl:message name="GetGroupsResponse">
    <wsdl:part name="parameters" element="ns0:GetGroupsResponse"/>
</wsdl:message>

<wsdl:portType name="MyProjectPortType">
    <wsdl:operation name="GetMessages">
        <wsdl:input message="axis2:GetMessagesRequest" wsaw:Action="urn:GetMessages"/>
        <wsdl:output message="axis2:GetMessagesResponse" wsaw:Action="urn:GetMessagesResponse"/>
    </wsdl:operation>
    <wsdl:operation name="GetGroups">
        <wsdl:input message="axis2:GetGroupsRequest" wsaw:Action="urn:GetGroups"/>
        <wsdl:output message="axis2:GetGroupsResponse" wsaw:Action="urn:GetGroupsResponse"/>
    </wsdl:operation>
</wsdl:portType>

Another thing you can check is to verify that the wsdl you got from java2wsdl is the same that is generated by axis2. Unless you have changed the default setting of "useoriginalwsdl" in your services.xml, these wsdls can "look" different. I never had to perform a java2wsdl manually to get my webservice to function correctly...

您可以检查的另一件事是验证您从 java2wsdl 获得的 wsdl 是否与axis2 生成的相同。除非您在 services.xml 中更改了“useoriginalwsdl”的默认设置,否则这些 wsdl 可以“看起来”不同。我从来不需要手动执行 java2wsdl 来让我的网络服务正常运行......

So basically, hit your service URL in a browser and tack on the ?wsdl at the end of the url...you should get a wsdl for comparison sake.

所以基本上,在浏览器中点击你的服务 URL 并在 URL 末尾添加 ?wsdl ......为了比较起见,你应该得到一个 wsdl。

Also, have your client generate stubs from the server's wsdl instead of one that is generated by java2wsdl (assuming you originally used the wsdl from java2wsdl). Again, we never had to pass a manually generated wsdl around to anyone...they just simply consumed the dynamically generated one from the server...

此外,让您的客户端从服务器的 wsdl 生成存根,而不是由 java2wsdl 生成的存根(假设您最初使用的是 java2wsdl 中的 wsdl)。再说一次,我们不必将手动生成的 wsdl 传递给任何人……他们只是简单地使用了从服务器动态生成的 wsdl……

回答by Tim Reddy

Have you tried to send a request like this?

您是否尝试过发送这样的请求?

<?xml version='1.0' encoding='utf-8'?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soap:Body>
    <request>
      <GetGroups xmlns="http://stws/xsd">
        <serialcode>123456-654321</serialcode>
      </GetGroups>
    </request>
  </soap:Body>
</soap:Envelope>

All my requests have the requesttag before the actual request parameters.

我的所有请求在实际请求参数之前都有请求标记。

回答by HalliHax

try this: 123456-654321

试试这个:123456-654321

Put xmlns="" into the parameter tag. I have the same problem and I don't know what I can modify in order to receive the parameter whitout the xmlnx.

将 xmlns="" 放入参数标记中。我有同样的问题,我不知道我可以修改什么来接收没有 xmlnx 的参数。

回答by Ben Howell-Thomas

I have another fix. I eventually discovered that if I didn't let my IDE (Netbeans 6.8) generate the WSDL then the web service worked. Alternatively if I deleted it, unticked the generate option and re-deployed then it worked.

我有另一个修复。我最终发现,如果我不让我的 IDE(Netbeans 6.8)生成 WSDL,那么 Web 服务就可以工作。或者,如果我删除它,取消选中生成选项并重新部署,然后它就可以工作了。

Comparing a Netbeans generated WSDL to a server generated one I noticed the following differences :

将 Netbeans 生成的 WSDL 与服务器生成的 WSDL 进行比较,我注意到以下差异:

  • xmlns:ns0="http:///xsd"
  • target namespace at the end of the wsdl:definitions tag had a trailing slash
  • ns0 used to choose the elements that make up the wsdl:message tag
  • xmlns:ns0="http:///xsd"
  • wsdl:definitions 标签末尾的目标命名空间有一个尾部斜杠
  • ns0 用于选择组成 wsdl:message 标签的元素

Removing all these and re-deploying worked!

删除所有这些并重新部署工作!

回答by Ngetha

If you had a complex attribute this is how you could do it

如果你有一个复杂的属性,这就是你可以做到的

Before

 <xs:element name="getMyMenu">
            <xs:complexType>
                <xs:sequence>
                    <xs:element minOccurs="0" name="number" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="var2" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="var3" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="var4" nillable="true" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>

After

            <xs:complexType name="getMyMenu">
                <xs:sequence>
                    <xs:element minOccurs="0" name="number" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="var2" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="var3" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="var4" nillable="true" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>

And then change this

然后改变这个

<wsdl:message name="getMyMenuRequest">
    <wsdl:part name="parameters" type="ns0:getUssdMenu"/>
</wsdl:message>

To

<wsdl:message name="getMyMenuRequest">
    <wsdl:part name="parameters" type="ns:getUssdMenu"/>
</wsdl:message>

That should be it! It did it for me....

应该是这样!它为我做到了......