我正在尝试将 map 属性添加到用户域
class User {
String id
String username
String password
....
Map<String, Follower> followers
//i've tried also without embeded and got the same error. also tried follower insead of followers
static embedded = ['followers']
}
class Follower {
String name
List<String> interests
}
我有一个实现保存方法的 Restful Controller
@Transactional
@Secured(['permitAll'])
def save(User user){
user.id = new ObjectId().encodeAsBase64()
user = user.insert(flush: true)
respond
}
遗憾的是我遇到了一个异常(exception):
java.lang.IllegalStateException: Cannot convert value of type [com.google.gson.JsonObject] to required type [Follower] for property 'followers[532dbe3b8fef86ebe3e64789]': no matching editors or conversion strategy found Around line 26 ofUserController.groovy
第 26 行是:user = user.insert(flush: true)
示例 json 请求:
{
username : "roy",
password : "123456",
followers : {
"532dbe3b8fef86ebe3e64789" : {
name : "Roy",
interests : ["math"]
}
}
}
任何帮助将不胜感激
谢谢!
罗伊
最佳答案
您正在尝试将 JSONObject 保存为 Follower 实例。解决该问题的直接方法是将它们手动转换为 Follower 实例:
def save(User user){
user.id = new ObjectId().encodeAsBase64()
user.followers = user.followers.collect{ new Follower( it ) }
user = user.insert(flush: true)
respond
}
如果你有更多这样的情况,你应该注册一个属性编辑器来转换 JSON <-> Follower
关于java - Grails mongodb 映射类型给出非法状态异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22580980/