Grails - 为每个响应添加标题 [英] Grails - add header to every response
问题描述
我怎样才能添加一个响应头文件 - 比如X-Time,它看起来像
X-Time:112
其中给定的值将是响应进行处理的时间(以毫秒为单位)?有没有一种非常简单的方式将其添加到Grails应用程序中?不是我想永久留下的东西,而是在开发我的应用程序时很高兴。 解决方案
您可以在 Filter之后使用。
// grails -app / conf / MyFilters.groovy $ b $ class MyFilters {
def filters = {
addHeader(uri:'/ *'){
after = {
response。 setHeader('X-Time',value)
}
}
}
}
编辑:
要真正计算时间,使用< a href =http://download.oracle.com/javaee/6/api/javax/servlet/Filter.html =noreferrer> javax.servlet.Filter
而不是Grails过滤器。
$ b src / groovy / com / example / myproject / MyFilter.groovy
$ b
package com.example.myproject
import javax.servlet。*
class MyFilter implements Filter {
void doFilter(ServletRequest请求,ServletResponse响应,FilterChain链){
def start = System.currentTimeMillis()
chain.doFilter(request,response)
def elapsed = System.currentTimeMillis() - 开始
response.setHeader('X-Time',以字符串形式传递)
}
void init(FilterConfig config){}
void destroy(){}
}
src / templates / war /web.xml
(如果src / templates不在您的源代码树中,请运行grails install-templates)
<滤光器>
< filter-name>定时器< / filter-name>
< filter-class> com.example.myproject.MyFilter< / filter-class>
< / filter>
< filter-mapping>
< filter-name>定时器< / filter-name>
< url-pattern> / *< / url-pattern>
< / filter-mapping>
使用 javax.servlet.Filter $ c $因此你不必分开你的之前和之后动作,因此可以保持贯穿整个过滤器链的开始时间。 servlet执行。
补充说明:
对我来说,希望将服务器已用执行时间作为响应头返回。也许你有这样一个体面的理由,但在大多数情况下,我会A)更关心总的往返时间(客户端观察到),或者B)记录服务器上的执行时间自己的系统管理/度量指标。
How could I add a response header - say X-Time that would look something like
X-Time: 112
Where the value given would be the time in milliseconds that the response took to process? Is there a really simple way to add this to an Grails app? Not something I want to leave on permanently but would be nice to have while developing my app.
To simply add a header to the response, you can use an after
Filter.
// grails-app/conf/MyFilters.groovy
class MyFilters {
def filters = {
addHeader(uri: '/*') {
after = {
response.setHeader('X-Time', value)
}
}
}
}
Edit:
To actually compute the time, it'd probably be more proper to use a javax.servlet.Filter
instead of a Grails filter.
src/groovy/com/example/myproject/MyFilter.groovy
package com.example.myproject
import javax.servlet.*
class MyFilter implements Filter {
void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
def start = System.currentTimeMillis()
chain.doFilter(request, response)
def elapsed = System.currentTimeMillis() - start
response.setHeader('X-Time', elapsed as String)
}
void init(FilterConfig config) { }
void destroy() { }
}
src/templates/war/web.xml (run grails install-templates if src/templates isn't already in your source tree)
<filter>
<filter-name>timer</filter-name>
<filter-class>com.example.myproject.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>timer</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
The reason for using the javax.servlet.Filter
is so you don't have to separate out your "before" and "after" actions, and can therefore hold onto the start time throughout the entire filter chain & servlet execution.
Supplementary note:
To me, it seems strange to want to return the server elapsed execution time as a response header. Perhaps you have a decent reason for doing it, but in most cases I'd A) either be more concerned about total round-trip time (as observed by the client), or B) be logging elapsed execution times on the server for my own system administration/metrics purposes.
这篇关于Grails - 为每个响应添加标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!