简体   繁体   English

Groovy WsClient-地图类型无法正确解析

[英]Groovy WsClient - Map types does not resolved properly

I'm using the following lines of code to call the web-service below: 我正在使用以下代码行来调用下面的Web服务:

 def wsdl = 'http://somewhere.com/services/msgService?wsdl'  
 proxy = new WSClient(wsdl, this.class.classLoader)  
 proxy.initialize()  

 def msg = proxy.create("com.somwhere.test.api.MsgService")
 msg.applicationName = "APP1"  
 msg.clientId = 5  
 msg.additionalProperties = [test:3]  

for web-service 用于网络服务

  <xs:schema targetNamespace="http://somewhere.com/test/api/MsgService" version="1.0" xmlns:tns="http://somewhere.com/test/api/MsgService" xmlns:xs="http://www.w3.org/2001/XMLSchema">  
 <xs:element name="sendMessage" type="tns:sendMessage"/>  
   <xs:complexType name="sendMessage">  
    <xs:sequence>  
     <xs:element minOccurs="0" name="mRequest" type="tns:mServiceRequest"/>  
    </xs:sequence>  
   </xs:complexType>  
   <xs:complexType name="mServiceRequest">  
    <xs:sequence>  
     <xs:element name="additionalProperties">  
      <xs:complexType>  
       <xs:sequence>  
        <xs:element maxOccurs="unbounded" minOccurs="0" name="entry">  
         <xs:complexType>  
          <xs:sequence>  
           <xs:element minOccurs="0" name="key" type="xs:string"/>  
           <xs:element minOccurs="0" name="value" type="xs:anyType"/>  
          </xs:sequence>  
         </xs:complexType>  
        </xs:element>  
       </xs:sequence>  
      </xs:complexType>  
     </xs:element>  
     <xs:element minOccurs="0" name="applicationName" type="xs:string"/>  
     <xs:element minOccurs="0" name="clientId" type="xs:long"/>  
     .......  
    </xs:sequence>  
   </xs:complexType>  
  </xs:schema>  

But get the following error: 但是出现以下错误:

Caught: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '{test=3}' with class 'java.util.LinkedHashMap' to class 'com.somwhere.test.api.MsgService$AdditionalProperties'

However, when the additionalProperties are an empty map, ie [:] it works fine. 但是,当AdditionalProperty是一个空映射时,即[:],它可以正常工作。

What am I doing wrong? 我究竟做错了什么? How must I format the map, or what other object do I need to use in order for it to work? 我必须如何格式化地图,或者我需要使用什么其他对象才能使其工作?

This is almost a year old... I hope you already found the answer somewhere else. 这已经快一年了...我希望您已经在其他地方找到了答案。
Just for the record I'll add what I think 仅作记录,我将添加我的想法

The client should have generated a class with properties named key and value , just instantiate it with the normal create() and set said properties. 客户端应该已经生成了一个具有名为keyvalue属性的类,只需使用常规的create()实例化它并设置所述属性即可。
The field additionalProperties might be a simple list of said 'entries' or another class wrapping the list, in which case you have to create() it also. 字段additionalProperties可能是所述“条目”的简单列表,也可能是包含该列表的另一个类,在这种情况下,您还必须create()它。

The best thing to do is check the list of generated classes when generating the client, creating each one and dump()ing them to see the structure. 最好的办法是在生成客户端时检查生成的类的列表,创建每个类并dump()将其查看结构。
Be prepared to write something like this. 准备写这样的东西。

new groovyx.net.ws.WSClient(
    "http://localhost/service?wsdl",
    this.class.classLoader).with {
  initialize()

  def wrapper = create('defaultnamespace.MapWrapper')
  wrapper.map = create('defaultnamespace.ArrayOfMapWrapperEntry')
  wrapper.map.mapWrapperEntry = [key1:'value1',key2:'value2'].collect{k,v->
    def entry = create('defaultnamespace.MapWrapperEntry')
    entry.key = k
    entry.value = v
    entry
  }

  send wrapper    
}

We decided to connect to another gateway that uses REST instead of wsdl since I didn't get it to work. 我们决定连接到另一个使用REST而不是wsdl的网关,因为我没有使用它。 I haven't tried jpertinos solution, but it looks promising. 我还没有尝试过jpertinos解决方案,但是看起来很有希望。

However, I'm closing this ticket. 但是,我要关闭这张票。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM