【问题标题】:simple grails GSON plugin usage简单的 grails GSON 插件使用
【发布时间】:2013-06-25 19:48:16
【问题描述】:

我正在尝试使用这个超级好的插件: 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
}

【问题讨论】:

  • ToolType 长什么样子?先看一下Serialization。
  • OK - 这就是解决方案。我没有意识到 static belongsTo = [tolltype: ToolType];与静态的 belongsTo = ToolType 不同;
  • @dmahapatro 你想回答吗?你是对的。
  • 只要有帮助我就很好。 :-)

标签: grails gson


【解决方案1】:

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

之前(不工作):

class Album {
    String title
    static belongsTo = Artist
}

之后(工作):

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 2014-10-26
    • 2016-03-02
    • 2016-10-23
    • 1970-01-01
    相关资源
    最近更新 更多