我正在尝试 JSON 视图,而不是在使用 @Resource 的域类之上,而是通过创建一个 RestfulController 并尝试使用 JSON 视图呈现它。我已经在构建配置中添加了所有相关的依赖项。
我有一个像这样的域 Post 类(我不想直接公开)
class Post implements Serializable {
Map comments
User user
Venue venue
String description
Rating rating //should this be an enum?
LocalDateTime dateCreated
LocalDateTime lastUpdated
static belongsTo = [user:User]
static hasOne = [rating:Rating]
static constraints = {
venue nullable:true
comments nullable:true
description nullable:true
rating nullable:true, lazy:false
}
static mapping = {
//set the sort order for Posts - default using newest post first
sort dateCreated :"desc"
}
}
所以我然后像这样创建了一个默认的 RestfulController:
class PostRestController extends RestfulController {
static responseFormats = ["json", "xml"]
//constructor - tells rest controller which domain class to scaffold
PostRestController() {
super (Post)
}
}
我没有在这里覆盖任何默认的脚手架方法。
当我使用休息客户端访问默认值时(我已/api/posts (resources: postRest
在 UrlMappings 中映射)。当我使用我的 REST 客户端访问 URL 时,我得到了帖子的完整转储(包括保留在地图中的评论字段) - 这在我的 REST 客户端中看起来像这样 - 一切正常:
[
{
"id": 1,
"comments": {
"view": "lovely"
},
"dateCreated": {
"class": "java.time.LocalDateTime",
"dayOfMonth": 7,
"dayOfWeek": {
"enumType": "java.time.DayOfWeek",
"name": "TUESDAY"
},
"dayOfYear": 66,
"hour": 19,
"minute": 15,
"month": {
"enumType": "java.time.Month",
"name": "MARCH"
},
"monthValue": 3,
"nano": 263000000,
"second": 10,
"year": 2017,
"chronology": {
"calendarType": "iso8601",
"class": "java.time.chrono.IsoChronology",
"id": "ISO"
}
},
"description": null,
"lastUpdated": {
"class": "java.time.LocalDateTime",
"dayOfMonth": 7,
"dayOfWeek": {
"enumType": "java.time.DayOfWeek",
"name": "TUESDAY"
},
"dayOfYear": 66,
"hour": 19,
"minute": 15,
"month": {
"enumType": "java.time.Month",
"name": "MARCH"
},
"monthValue": 3,
"nano": 263000000,
"second": 10,
"year": 2017,
"chronology": {
"calendarType": "iso8601",
"class": "java.time.chrono.IsoChronology",
"id": "ISO"
}
},
"rating": null,
"user": {
"id": 1
},
"venue": null
}
],
然后我尝试在grails-app/views/postRest
文件夹中添加 JSON 视图。
我做了一个非常简单的模板_post.gson,如下所示:
model {
Post post
}
json {
comments post.comments
description post.description
//rating post.rating
userWhoPosted "${post?.user}"
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MMM-dd")
def when = post.dateCreated.format(formatter)
created when
}
然后我添加了一个index.gson
来渲染模板:
model {
List<Post> postList
}
//call the template to iterate over the postList to produce the output
json g.render(postList)
这会通过此堆栈跟踪和 REST 客户端的 500 错误中断服务器。如果我注释掉与用户相关的 in_post.gson 行,一切正常。把它留在里面,它会失败:
Caused by: grails.views.ViewRenderException: Error rendering view: null
at grails.views.AbstractWritableScript.writeTo(AbstractWritableScript.groovy:43)
at grails.views.mvc.GenericGroovyTemplateView.renderMergedOutputModel(GenericGroovyTemplateView.groovy:73)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
at grails.views.mvc.renderer.DefaultViewRenderer.render(DefaultViewRenderer.groovy:111)
at grails.artefact.controller.RestResponder$Trait$Helper.internalRespond(RestResponder.groovy:188)
at grails.artefact.controller.RestResponder$Trait$Helper.respond(RestResponder.groovy:62)
at grails.rest.RestfulController.index(RestfulController.groovy:64)
at grails.transaction.GrailsTransactionTemplate$2.doInTransaction(GrailsTransactionTemplate.groovy:96)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
at grails.transaction.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:93)
... 4
如果我注释掉 post.user (和评级参考),它可以正常工作,但是当我尝试发布 post.user 时,它会因上述原因而失败。文档中有一条关于确保您的查询使用 fetch join 拉取 refs 的注释 - 所以我尝试提供覆盖以确保我返回了 fetch join - 我得到的只是空的返回给客户端:
class PostRestController extends RestfulController {
static responseFormats = ["json", "xml"]
//constructor - tells rest controller which domain class to scaffold
PostRestController() {
super (Post)
}
def index() {
Collection<Post> res = Post.list([fetch:[user:"join",rating:"join"]])
res
}
}
为什么当我在没有 JSON 视图的情况下工作时它工作正常,而当我使用 JSON 视图时我无法获得包括引用在内的输出?我检查了列表请求,它在调试器中成功返回了列表 - 但在渲染中中断。
如果我可以让它工作,Grails 3.2.6 上的 JSON 视图看起来很不错。