1
Groovy中有着简洁的语法保湿一个POGO有地图,如:使用Map实例化@Immutable Groovy对象?
class Person {
Address address
}
class Address {
String city
}
Person p = new Person([address: [city: 'Toronto']])
assert p.address.city == 'Toronto'
即使深度嵌套样板工程!我试着用@Immutable模型这样做,但没有成功:
@groovy.transform.Immutable
class Person {
Address address
}
@groovy.transform.Immutable
class Address {
String city
}
//works:
Address a = new Address('Toronto')
Person p = new Person(a)
assert p.address.city == 'Toronto'
//not works:
Person p = new Person([address: [city: 'Toronto']])
// ==> java.lang.RuntimeException: @Immutable processor doesn't know how to handle field 'address' of type 'java.util.LinkedHashMap' while constructing class Person.
做这种特别真棒从JSON去 - >地图 - > POGO。
任何想法如何?
太棒了!非常感谢: – crazy4groovy
这是否意味着您将选择我的答案作为正确答案(绿色检查)? (感谢upvote,但这不完全一样) – BalRog
是的,它现在完成了:) – crazy4groovy