0

使用 json 视图深度渲染域对象时出现堆栈溢出。

我创建了一个客户域对象和站点域对象,其中客户有许多站点。

我在 bootsrap 中创建了两个客户和一个站点,并将该站点分配给了第一个客户。

我已经使用 gorm 数据服务从接口创建了一个 customerService 实例。

根据 json 视图文档(第 2.5.2 节),我保证当我在控制器上使用索引操作时,我会为站点执行连接获取,并确保我在一个休眠会话中拥有客户列表和站点以传递给看法。

在调用 'json g.render (customer, [deep:true])' 的 _customerRest.gson 视图中。我有一个 _siteRest.gson 模板用于站点域对象/控制器配对(这有效 - 当我只使用休息来获取站点或特定站点时,它会正确呈现)

当我用 Postman 调用我的“/api/customers”uri 时,我得到一个重复的 json 输出,像这样

[{"id":1,"sites":[{"id":1,"name":"Canary Wharf","customer":{"id":1,"sites":[{"id":1,"name":"Canary Wharf","customer":{"id":1,"sites":[{"id":1,"name":"Canary Wharf","customer":{"id":1,"sites":[{"id":1,"name":"Canary Wharf","customer":{"id":1,"sites":[{"id":1,"name":"Canary Wharf","customer":{"id":1,"sites":[{"id":1,"name":"Canary Wharf","customer":{"id":1,"sites":[{"id":1,"name":"Canary Wharf","customer":{"id":1,"sites":[{"id":1,"name":"Canary Wharf","customer":{"id":1,"sites":[{"id":1,"name":"Canary Wharf","customer":{"id":1,"sites":[{"id":1,"name":"Canary Wharf","customer":{"id":1,"sites":[{"id":1,"name":"Canary Wharf","customer":{"id":1,"sites":[{"id":1,"name":"Canary Wharf","customer":{"id":1...

然后像这样的stackoverflow

Grails application running at http://localhost:8080 in environment: development
CustomerRest.customerRest.index method invoked
param map to customerService contains [controller:customerRest, action:index, max:10, fetch:[sites:join]]
service returned collection  [ttrestapi.model.Customer : 1, ttrestapi.model.Customer : 2]
2018-03-20 16:36:21.504 ERROR --- [nio-8080-exec-4] o.g.web.errors.GrailsExceptionResolver   : StackOverflowError occurred when processing request: [GET] /api/customers
Stacktrace follows:

java.lang.reflect.InvocationTargetException: null
    at org.grails.core.DefaultGrailsControllerClass$ReflectionInvoker.invoke(DefaultGrailsControllerClass.java:211)
    at org.grails.core.DefaultGrailsControllerClass.invoke(DefaultGrailsControllerClass.java:188)
    at org.grails.web.mapping.mvc.UrlMappingsInfoHandlerAdapter.handle(UrlMappingsInfoHandlerAdapter.groovy:90)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
    at org.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:77)
    at org.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:67)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: grails.views.ViewRenderException: Error rendering view: 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:98)
    at ttrestapi.model.CustomerRestController.index(CustomerRestController.groovy:27)
    ... 14 common frames omitted
Caused by: grails.views.ViewRenderException: Error rendering view: null
    at grails.views.AbstractWritableScript.writeTo(AbstractWritableScript.groovy:43)
    at grails.plugin.json.view.api.internal.DefaultGrailsJsonViewHelper$6.writeTo(DefaultGrailsJsonViewHelper.groovy:792)
    at grails.plugin.json.view.JsonViewWritableScript.json(JsonViewWritableScript.groovy:123)
    at grails.plugin.json.view.JsonViewWritableScript.json(JsonViewWritableScript.groovy:146)
    at ttRestApi_customerRest_index_gson.run(ttRestApi_customerRest_index_gson:9)
    at grails.plugin.json.view.JsonViewWritableScript.doWrite(JsonViewWritableScript.groovy:28)
    at grails.views.AbstractWritableScript.writeTo(AbstractWritableScript.groovy:40)
    ... 20 common frames omitted
Caused by: java.lang.StackOverflowError: null
    at java.io.InputStream.<init>(InputStream.java:45)
    at java.util.zip.ZipFile$ZipFileInputStream.<init>(ZipFile.java:711)
    at java.util.zip.ZipFile.getInputStream(ZipFile.java:375)
    at java.util.jar.JarFile.getInputStream(JarFile.java:447)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:454)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at grails.plugin.json.builder.StreamingJsonBuilder$StreamingJsonDelegate.curryDelegateAndGetContent(StreamingJsonBuilder.java:809)
    at grails.plugin.json.builder.StreamingJsonBuilder$StreamingJsonDelegate.writeObject(StreamingJsonBuilder.java:775)
    at grails.plugin.json.builder.StreamingJsonBuilder$StreamingJsonDelegate.writeCollectionWithClosure(StreamingJsonBuilder.java:766)
    at grails.plugin.json.builder.StreamingJsonBuilder$StreamingJsonDelegate.writeObjects(StreamingJsonBuilder.java:706)
    at grails.plugin.json.builder.StreamingJsonBuilder$StreamingJsonDelegate.call(StreamingJsonBuilder.java:610)
    at .....

如果我删除 [deep:true] 选项,我的 json 将呈现,但不会在输出中显示站点详细信息 - 只是站点 id 作为子项。

此示例项目的最新版本位于 github 上:https ://github.com/woodmawa/ttRestApi

有没有其他人遇到过这个问题,这是我的模板 gson 某处的错误吗?

PS。如果将渲染命令更改为 json g.render (customer , [expand:['sites']]) - 这似乎可以正常工作。但 [deep:true] 选项如上所示失败。

我在 CustomerRestController 中的索引操作看起来像这样,其中包含调试语句

    def index(Integer max) {
        println "CustomerRest.customerRest.index method invoked"
        params.max = Math.min(max ?: 10, 100)
        params.put("fetch", [sites:"join"])   //force join fetch on sites
        println "param map to customerService contains $params"

        Collection result = customerService.list(params)
        println "service returned collection  $result"

 //       respond customerService.list(params), [ttrestapi.model:[customerCount: customerService.count()], view:"fred"]
        respond result, [model:[customerCount: customerService.count()]]
    }

当我查看 customerService.list 查询的结果时,我通过 fetch:[sites:"join"] 查询获得了第一个客户和他的单个站点,正如预期的那样

我的 _customerRest.gson 看起来像

    import ttrestapi.model.Customer

    model {
        Customer customer
    }

    json g.render (customer , [deep:true])

和相应的站点 _siteRest.gson 看起来像这样

import ttrestapi.model.Site

model {
    Site site
}

json jsonapi.render (site)
4

1 回答 1

1

我在https://github.com/grails/grails-views/issues/165提交了一份错误报告。

https://github.com/jeffbrown/williamwoodman/blob/86b0bed52fd00bb09cee22e49234797ef44f3a35/grails-app/views/customer/_customer.gson的模板如下所示:

import williamwoodman.Customer

model {
    Customer customer
}

json g.render(customer, [deep: true])

这将触发错误。根据您希望呈现的 JSON 的外观,您有多种选择。您可以做的一件事是编写您的模板,使其看起来像这样:

import williamwoodman.Customer

model {
    Customer customer
}

json {
    id customer.id
    name customer.name
    sites g.render(customer.sites ?: [])
}

在该示例项目中,将模板更改为如下所示,然后向http://localhost:8080/customer发送请求将生成如下所示的 JSON:

[{"id":1,"name":"Customer One",
  "sites":[{"id":1,"name":"Site One","customer":{"id":1}},       
           {"id":3,"name":"Site Three","customer":{"id":1}},
           {"id":2,"name":"Site Two","customer":{"id":1}}]}]

您可能希望customer从各个site地图中排除,也可能不希望。

我希望这会有所帮助。

于 2018-03-21T02:42:52.563 回答