本文介绍了Grails - 为每个响应添加标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我怎样才能添加一个响应头文件 - 比如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)} } } } 编辑: 要真正计算时间,使用 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 补充说明: 对我来说,希望将服务器已用执行时间作为响应头返回。也许你有这样一个体面的理由,但在大多数情况下,我会A)更关心总的往返时间(客户端观察到),或者B)记录服务器上的执行时间自己的系统管理/度量指标。 How could I add a response header - say X-Time that would look something likeX-Time: 112Where 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.groovyclass 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.groovypackage com.example.myprojectimport 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 - 为每个响应添加标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-11 04:49