简体   繁体   中英

Groovy WsClient - Map types does not resolved properly

I'm using the following lines of code to call the web-service below:

 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.

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.
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.

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.
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. I haven't tried jpertinos solution, but it looks promising.

However, I'm closing this ticket.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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