Grails / Groovy - Domain Object - Map of its Properties

前端 未结 3 2071
无人及你
无人及你 2021-01-18 06:18

How can I get a map of the key/values of only the user-defined properties on one of my domain objects?

Problem is if I do this myself, I get my properties plus class

3条回答
  •  没有蜡笔的小新
    2021-01-18 07:04

    Try this

    class Person{
        String name
        String address
    }
    
    def filtered = ['class', 'active', 'metaClass']
    
    def alex = new Person(name:'alex', address:'my home')
    
    def props = alex.properties.collect{it}.findAll{!filtered.contains(it.key)}
    
    props.each{
        println it
    }
    

    It also works if you use alex.metaClass.surname = 'such'. This property will be displayed in the each loop

提交回复
热议问题