2

我正在尝试使用过滤器更改我的 Grails 1.3.7 应用程序中的 JSON 响应,但它们没有按我预期的方式工作。我想做的是render myobject as JSON在我的操作中使用类似的东西,然后在过滤器中做这样的事情:

jsonPostProcess(controller:'json', action:'*') {
    after = {
        if (myCustomLogicHere) {
            return false // render empty string
        } else if (more logic) {
            // write something else to the response
        }
    }
}

after实际发生的是响应在块执行之前被发回。也是如此afterView

有没有办法完成我正在尝试做的 Grails 方式?

4

2 回答 2

1

myobject从控制器返回,然后render从过滤器调用,例如:

class JsonController {

    someAction = {

        ...
        myobject
    }
}

jsonPostProcess(controller:'json', action:'*') {
    after = {

        myobject ->

        if (myCustomLogicHere) {
            return false // render empty string
        } else if (more logic) {
            // write something else to the response
        }

        render myobject as JSON
    }
}
于 2012-07-10T23:46:49.553 回答
0

@JonoB 的答案几乎是正确的:

从控制器返回 myobject,然后从过滤器调用渲染,例如:

class JsonController {
    someAction = {
         //...some logic
         return [myobject: myobject]
    }
}

并在过滤器中

jsonPostProcess(controller:'json', action:'*') {
    after = {
        Map model ->
            //... my custom logic here
            render model.myobject as JSON
            return false
    }
}
于 2016-02-02T16:14:08.823 回答