简体   繁体   中英

How do I get declared properties in Grails domain objects?

I'm trying to convert Grails domain objects to Map. I have a code that do it for POGOs using Apache Commons BeanUtils' PropertyUtils.describe . However, using the same code on domain objects just doesn't work because of all the stuff added by the Grails framework. I want to be able to convert any object to a Map with only the declared fields just like how Grails converts domain objects to JSON. What features can help me achieve this?

I would suggest something like this inside your domain class:

    public Map asMap() {
        this.class.declaredFields.findAll { !it.synthetic }.collectEntries {
          [ (it.name):this."$it.name" ]
        }
    }

There are other questions about the same problem, did you try this? Grails / Groovy - Domain Object - Map of its Properties

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM