简单的Grails GSON插件用法

时间:2013-06-25 19:48:17

标签: grails gson

我正在尝试使用这个超级好的插件: https://github.com/robfletcher/grails-gson/blob/master/test/apps/gson-test/grails-app/controllers/grails/plugin/gson/test/AlbumController.groovy

因为默认的GRAILS JSON不会展开相关项目。

然而,当我尝试它时,它失败了。

现在,当我这样做时,它有效:

def levelJson() {

    render ToolType.list(params) as JSON
}

这失败了:

def levelJson() {

    render ToolType.list(params) as GSON
}

错误:

ERROR errors.GrailsExceptionResolver  - UnsupportedOperationException occurred when processing request: [GET] /authtools/toolType/levelJson
Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter?. Stacktrace follows:
Message: Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter?

类:

class Artist {
    String name
    static hasMany = [albums: Album]
}

class Album {
    String title
    static belongsTo = Artist
}

1 个答案:

答案 0 :(得分:0)

dmahapatro指出的解决方案是修改数据类belongsTo

之前(不工作):

class Album {
    String title
    static belongsTo = Artist
}

之后(工作):

class Album {
    String title
    static belongsTo = [artist: Artist]
}