【问题标题】:Passing Hashmap as parameter in EJB3 WebService在 EJB3 WebService 中将 Hashmap 作为参数传递
【发布时间】:2012-08-23 16:45:07
【问题描述】:

我已经将 EJB3 bean 的 api 公开为 WebService,它以 java 的 HashMap 作为参数,但是在从 hashMap 获取 WebService Bean 的值时,我得到了 null 值。

@EJB(mappedName = "ODBillGenerationSession",name = "ODBillGenerationSession")
IODBillGenerationSessionRemoteHome iodBillGenerationSessionRemoteHome = null;
IODBillGenerationSessionRemote iodBillGenerationSessionRemote = null; 
public String echo(@WebParam(name="hashMap") HashMap hashMap) {
    return "Hello "+hashMap.get("name");
}

我从 SOAP UI 执行的请求 XML 如下:-

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:int="http://interfaces.billingengine.ws.billingengine.elitecore.com/"> <soapenv:Header/>    <soapenv:Body>
      <int:echo>
         <!--Optional:-->
         <arg0>Test</arg0>
      </int:echo>    </soapenv:Body> </soapenv:Envelope>

当我传递 java String 对象时,相同的 Web 服务工作正常。

我想我在这里遗漏了一些我搜索但没有找到的注释。

<definitions name="ODBillGenerationWsSessionFacadeService" targetNamespace="http://session.billingengine.ws.billingengine.elitecore.com/">
<import location="http://10.105.1.6:8180/odbillgeneration-ws/ODBillGenerationWsSessionFacade?wsdl&resource=IODBillGenerationWsSessionRemote_PortType6834015600007002099.wsdl" namespace="http://interfaces.billingengine.ws.billingengine.elitecore.com/"/>
    <service name="ODBillGenerationWsSessionFacadeService">
        <port binding="ns1:IODBillGenerationWsSessionRemoteBinding" name="ODBillGenerationWsSessionFacadePort">
            <soap:address location="http://10.105.1.6:8180/odbillgeneration-ws/ODBillGenerationWsSessionFacade"/>
        </port>
    </service>

正确的 WSDL

<definitions name="ODBillGenerationWsSessionFacadeService" targetNamespace="http://interfaces.billingengine.ws.billingengine.elitecore.com/">
    <types>
        <xs:schema targetNamespace="http://interfaces.billingengine.ws.billingengine.elitecore.com/" version="1.0">
            <xs:element name="echo" type="tns:echo"/>
            <xs:element name="echoResponse" type="tns:echoResponse"/>
            <xs:complexType name="echo">
                <xs:sequence>
                    <xs:element minOccurs="0" name="arg0" type="tns:hashMap"/>
                </xs:sequence>
            </xs:complexType>
            <xs:complexType name="hashMap">
                <xs:complexContent>
                    <xs:extension base="tns:abstractMap">
                        <xs:sequence/>
                    </xs:extension>
                </xs:complexContent>
            </xs:complexType>
            <xs:complexType abstract="true" name="abstractMap">
                <xs:sequence/>
            </xs:complexType>
            <xs:complexType name="echoResponse">
                <xs:sequence>
                    <xs:element minOccurs="0" name="return" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
        </xs:schema>
    </types>
    <message name="IODBillGenerationWsSessionRemote_echoResponse">
        <part element="ns1:echoResponse" name="echoResponse"/>
    </message>
    <message name="IODBillGenerationWsSessionRemote_echo">
        <part element="ns1:echo" name="echo"/>
    </message>
    <portType name="IODBillGenerationWsSessionRemote">
        <operation name="echo" parameterOrder="echo">
            <input message="ns1:IODBillGenerationWsSessionRemote_echo"/>
            <output message="ns1:IODBillGenerationWsSessionRemote_echoResponse"/>
        </operation>
    </portType>
    <binding name="IODBillGenerationWsSessionRemoteBinding" type="ns1:IODBillGenerationWsSessionRemote">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="echo">
            <soap:operation soapAction=""/>
            <input>
                <soap:body use="literal"/>
            </input>
            <output>
                <soap:body use="literal"/>
            </output>
        </operation>
    </binding>
</definitions>

【问题讨论】:

  • @Milijen :- 已粘贴 WSDL 但它不是手动创建的,它是由 JBOSS 5 生成的。
  • 是否有一些外部 XSD 或包含定义的东西?我想看看你的“hashMap”输入参数的映射。
  • 不,没有外部 XSD 或包含定义的东西。我已经使用了上面代码 sn-p 中提到的注释,其中我提到了 api 回显细节。
  • 好的,但是注释最终映射到某种配置中。外部客户端如何知道向您的 Web 方法发送什么样的参数?
  • 我可以看到您正在导入 IODBillGenerationWsSessionRemote_PortType6834015600007002099.wsdl 和 ODBillGenerationWsSessionFacade.wsdl,它们可能包含必需的定义。

标签: java web-services jakarta-ee jax-ws ejb-3.0


【解决方案1】:

您没有发送键值对,因此您的 Web 方法无法读取所需的键“名称”。用这个替换你的 SOAP 主体:

<soapenv:Body>
      <int:echo>
         <hashMap>
            <!--Zero or more repetitions:-->
            <arg0>
               <key>name</key>
               <value>someValue</value>
            </arg0>
         </hashMap>
      </int:echo>
   </soapenv:Body>

另外,将输入参数的类型从HashMap 更改为HashMap&lt;String,String&gt;。这应该可以解决问题。

【讨论】:

  • @Milijen :谢谢,但仍然无法正常工作......强制必须使用 标记否则会给出 NullPointerException。
  • 我已经更新了答案,尝试更改Web方法中的参数类型再试一次。
  • @Milijen:感谢您的努力。但值仍然为空。
  • 可以公开服务并发布网址吗?我想试试看问题出在哪里。
  • 请同时添加运行 Web 服务的 Metro 版本(或至少应用服务器版本)。
猜你喜欢
  • 1970-01-01
  • 2013-06-06
  • 2016-11-04
  • 2011-07-23
  • 2013-04-27
  • 1970-01-01
  • 2011-07-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多