Using bindData in services

11 / Aug / 2010 by Sachin 0 comments

In one of my projects, I was working on rest calls, and one of the requirement was such that I had to pass the XML to service and then parse it and bind the relevant details to an instance of a domain object. In essence I had to call bindData in service after creating a Map from the XML. I soon realised we could not call bindData in service, its a method injected by grails only in controllers. After searching for stuff on google.. I found the way out, it was simple All we have to do is

import org.codehaus.groovy.grails.web.metaclass.BindDynamicMethod

and then in your method do

BindDynamicMethod bind = new BindDynamicMethod()
args =  [objectInstance, map, [exclude:['prop1', 'prop2']]]
        bind.invoke(objectInstance,'bind',(Object[]) args)

Here is the Link which I referred JT’s Blog it will give you a little more detail regarding using bindData in services.

Hope it helps.

With Regards
Sachin Anand


Leave a Reply

Your email address will not be published. Required fields are marked *