Is there a way to make groovy ignore extra attributes in a map during object instantiation? Example:
class Banana{
String name
}
def params = [name:'someGuy', age:13]
new Banana(params)
In this example, groovy throws a No such property: age exception (obviously because age isn’t defined in the Banana class. Without resorting to manually mapping only the desired attributes from the map to the constructor of the Banana class, is there a way to tell Banana to ignore the extra attributes?
I noticed that Grails domain classes do not suffer from this problem, and I would like the same behavior here!
Thanks for your help and advice!
Unfortunately, there’s no built in way to do this in groovy. Grails does it by generating its own constructors for domain objects. A simple workaround is to use a constructor like this: