1

I have a SOAP method with 40+ parameters, I would like to pass an map rather than list all params in the method call. Here is a sample code:

Map input = [ Param1: 'Test1', Param2: 'Test2' ]

class WSClient {

    def post(String Param1, String Param2) {
        println "Param1: ${Param1}, Param2: ${Param2}"
    }
}

def client = new WSClient()

client.post(input)

Is there a way to "spread" map key/values to method formal parameters? Or is there any other way of doing this the groovy way?

This code above results in No signature of method: WSClient.post() is applicable for argument types: (java.util.LinkedHashMap)

Janusz Slota
  • 383
  • 1
  • 3
  • 17
  • Following Jeff's answer, using List will be a good approach because you can maintain an index matching the position of parameters. – dmahapatro May 07 '14 at 14:20
  • Are you on jdk8, by any chance? There is a new [`getParameters()`](http://stackoverflow.com/questions/21455403/how-to-get-method-parameter-names-in-java-8-using-reflection) reflection method which has the parameter name. – Will May 07 '14 at 14:39

1 Answers1

3

You can do it if you can make assumptions about the elements in the Map and their order. Something like this will work...

client.post(*(input.values() as List))

If you don't know the order of the entries or the number of entries, that isn't going to help.

Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47