ポリモーフィック クエリの結果から json-views をレンダリングしようとしています。オブジェクト マーシャラーでは完全に機能しますが、grails3 json-views では機能しません。あらゆる種類のことを試しましたが、json-views プラグインのバグのようです。
問題を明確にするための例を次に示します。
class Content {
String text
}
class BlogEntry extends Content {
URL url
}
class Book extends Content {
String ISBN
}
そして contentController で:
class ContentController extends RestfulController<Content> {
static responseFormats = ['json', 'xml']
ContentController() {
super(Content)
}
def index(Integer max) {
println '*CONTENT:INDEX*'
def contents= Content.findAll()
respond contents
}
}
json ビュー:
// /content/index.gson
model {
Iterable<Content> contentList
}
json tmpl.content(contentList ?: [])
// /content/_content.gson
model {
Content content
}
json g.render(content)
コントローラーの index メソッドから空の結果を取得します。私はviews-json:1.1.0.RC3でgrails 3.2.0.RC2を使用しています
カスタム オブジェクト マーシャラーを使用すると、期待される結果であるサブクラス タイプ (BlogEntry、Book) によって json の結果を取得できます。