Question

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)

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top