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