json - Handling PUT/POST requests in Grails when using custom converters -


i'm writing grail rest service , i've defined custom json converters. instance, have event class converted whenever such object requested client...

// in src/groovy/eventmarshaller.groovy class eventmarshaller {  void register(object config) {     config.registerobjectmarshaller(event) { event e ->         return [             id: e.id,             title: e.title,             description: e.description,             datecreated: e.datecreated.format('yyyy-mm-dd'),             creator: e.creator         ]     } } 

i register eventmarshaller within customobjectmarshaller expects named config parameter different rest api versions can accommodated...

// in src/groovy/customobjectmarshallers.groovy def register(string str) {     json.createnamedconfig(str) { cfg ->         marshallers.each {             it.register(cfg);         }     } }  // in bootstrap.groovy init section... def springcontext = webapplicationcontextutils.getwebapplicationcontext( servletcontext );     springcontext.getbean("customobjectmarshallers").register("v1"); 

this works charm whenever call get event object via rest api, domain object converted specified json format. example, event controller has index action...

def index(string v)  {     def configname = 'v' + (v ?: 1)      def listofevents = event.list()      // why work when converting domain object json?     json.use(configname) {                           respond listofevents     } } 

now need update , save actions when put , post commands received client. far have following in update action...

def update(long id, string v)  {     def configname = 'v' + (v ?: 1)      // how specify version? json.use(configname) doesn't seem work?     def jsonobj = json.parse(request);     def newevent = new event(jsonobj);     def evrequest = new eventrequest(jsonobj)             evrequest.errors.allerrors.each {         println     }     ... 

can explain how can tell update action config use when converting json domain object? (i've not seen example of online @ all.) also, datecreated field in newevent object null after json parsed. how can ensure if datecreated field present parsed it's original date object?

here's command object referenced in code...

/**------------------------------------------------------------ // event request command object  //----------------------------------------------------------**/ class eventrequest {     string id;     string title;     string description;     byte[] photo;     @bindingformat('yyyy-mm-dd')     date datecreated;      //------------------------------------------------------------      // import contraints event domain model     //------------------------------------------------------------     static constraints = {         importfrom event;     } } 

and event domain class maps onto...

import java.util.date; import org.emvigr.user.* import org.grails.databinding.bindingformat;  class event {      string title     string description     byte[] photo     @bindingformat('yyyy-mm-dd')     date datecreated      // can call user.addtoevents     static belongsto = [          creator : user      ]      static hasmany = [         portals : portal     ]      static constraints = {         title maxsize: 50         description nullable: true, maxsize: 300         photo nullable: true, maxsize: 2 * 1024 * 1024         datecreated nullable: true         portals nullable: true     }      // when events accessed sort datecreated (descending)     static mapping = {         sort datecreated:"desc"     }  } 

any appreciated!

thanks joshua moore information on namespaces , dmahapatro re date binding. having problems parsing date parameter in update realise because of way (mis)using command object.

for experiencing same problem, may particular 2.4.4, a class considered command object when used parameter of action according official docs. once amended update action so...

def update(eventrequestcommand cmd)  {                    cmd.errors.allerrors.each {         println     }      if (!cmd.haserrors()) {         def ev = event.get(cmd.id);         // save changes     }     else {         // handle error     } } 

the date somehow parsed. still don't know why doesn't work if create class of eventrequest in original code above , i've seen example command object not parameter. of thought date still parsed guess it's confusing aspects of grails.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -