Grails不会为JSON添加标头 [英] Grails doesn't add headers for json

查看:136
本文介绍了Grails不会为JSON添加标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在测试grails应用程序上使用过滤器添加自定义标头.我已经设置了这样的过滤器:

I'm trying to add custom headers using filters on a test grails app. I've setup a filter like this:

class MyFilters {
    def filters = {
        addHeader(uri: '/*') {
            after = { Map model ->
                response.setHeader('X-Test-Header', '123')
            }
        }
    }
}

如果我尝试访问控制器并检查标题:

If I try to access the controller and inspect the headers:

curl -I -H "Accept: text/html" -X GET 'http://localhost:8080/Test1/doSomething'

然后,我得到了附加的标头:X-Test-Header:123,但是如果我尝试使用"Accept:application/json"进行上述卷曲,则内容将以JSON格式显示,但没有任何附加的标头.

then, I get the additional header: X-Test-Header: 123 but if I try the above curl using "Accept: application/json" I get the content as JSON but I don't get any additional header.

我做错了什么?

我忘了提到我正在尝试新的grails REST功能,并且我的控制器扩展了RestfulController

I forgot to mention that I'm experimenting with the new grails REST functionality and my controller extends RestfulController

我做了一些其他的实验.我添加了另一个过滤器作为groovy类,并修改了web.xml(从本文-grails-add-header-to-every-response ,它不适用于application/json,但适用于text/html!是否有可能扩展

I've done some additional experiments. I've added another filter as groovy class and modified the web.xml (got the idea from this article - grails-add-header-to-every-response and it does not work for application/json but does work for text/html! Is it possible that extending the RestfulController bypasses the standard filters?

似乎添加标头适用于application/xml和text/xml,但不适用于application/json和text/json.我越看越多,这似乎是一个错误,而不是我在做错什么,因为我在全新VM上的全新项目中看到了完全相同的行为(以防我的开发机具有某种奇怪的配置) ).我将在JIRA上创建错误报告

It appears that adding headers works for application/xml and text/xml but not for application/json and text/json. The more I look at it, it appears as a bug and not that I'm doing something wrong because I see exactly the same behavior on a brand new project on a brand new VM (in case my dev machine has some sort of strange configuration). I'll create a bug report on JIRA

打开了 JIRA案例,当我知道更多信息时,我会更新此任务

Opened a JIRA case and when I know more, I'll update this task

基于 JIRA案例,看来这是一个错误它将在grails 2.3.4上得到纠正

Based on the JIRA case, it appears that it is a bug and it'll be corrected on grails 2.3.4

推荐答案

打开了 JIRA案例.看起来,"after"动作允许在渲染视图之前修改模型,但是response方法会渲染JSON并在输出响应后执行"after"动作(因此,修改响应为时已晚).因此,根据Graeme Rocher的说法,一种解决方法是将标头添加到之前"块或操作中,如下所示:

Opened a JIRA case. It appears that the "after" action allows modification of the model before rendering the view but the respond method renders the JSON and executes the "after" action after outputing the response (so, it's too late to modify the response). So, according to Graeme Rocher, a workaround is to add the headers in the "before" block or in the action as follows:

def index(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    response.setHeader('X-MyHeader', '123')
    respond Person.list(params), model:[personInstanceCount: Person.count()]
}

因此,这将纠正索引操作的行为

So, this will correct the behavior of the index action

这篇关于Grails不会为JSON添加标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆