royB
royB

Reputation: 12977

Grails mongodb map type gives illegalStateException

I'm trying to add a Map property to a User Domain

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
}

I Have a restful controller the implements the save method

@Transactional
@Secured(['permitAll'])
def save(User user){
    user.id = new ObjectId().encodeAsBase64()
    user = user.insert(flush: true)
    respond 
}

Sadly i'm getting an 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

line 26 is : user = user.insert(flush: true)

example json request:

{
    username : "roy",
    password : "123456",

    followers : {
        "532dbe3b8fef86ebe3e64789" : {
            name : "Roy",
            interests : ["math"]
        }
    }
}

Any help will be greatly appreciated

Thanks!

Roy

Upvotes: 0

Views: 108

Answers (1)

injecteer
injecteer

Reputation: 20699

you are trying to save JSONObject's as Follower instances. The straight forward way to solve the issue would be to convert those into Follower instances manually:

def save(User user){
  user.id = new ObjectId().encodeAsBase64()
  user.followers = user.followers.collect{ new Follower( it ) }
  user = user.insert(flush: true)
  respond 
}

if you have more of such cases, you should register a property editor for conversion JSON <-> Follower

Upvotes: 1

Related Questions