当前位置: 首页 > news >正文 许昌长葛网站建设制作设计个人网站设计规划书 news 2025/11/4 9:26:10 许昌长葛网站建设制作设计,个人网站设计规划书,中国企业100强排名,正规网站制作公司哪里有当使用Spring Boot时#xff0c;我们可以通过拦截器#xff08;Interceptor#xff09;和监听器#xff08;Listener#xff09;来实现对请求和响应的处理。拦截器和监听器提供了一种可插拔的机制#xff0c;用于在请求处理过程中进行自定义操作#xff0c;例如记录日志… 当使用Spring Boot时我们可以通过拦截器Interceptor和监听器Listener来实现对请求和响应的处理。拦截器和监听器提供了一种可插拔的机制用于在请求处理过程中进行自定义操作例如记录日志、身份验证、权限检查等。下面通过提供一个示例展示如何使用拦截器和监听器来记录请求日志。 首先我们创建一个简单的Spring Boot项目并添加所需的依赖。在这个示例中我们将使用Spring Boot Starter Web。 创建一个Spring Boot项目并添加依赖 创建一个新的Spring Boot项目可以使用Spring Initializrhttps://start.spring.io/进行初始化。在Dependencies中添加Spring Web依赖并生成项目。 创建拦截器 在项目中创建一个名为 RequestLoggingInterceptor 的类实现 HandlerInterceptor 接口。这个拦截器将记录请求的URL、HTTP方法和时间戳。 import org.springframework.web.servlet.HandlerInterceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class RequestLoggingInterceptor implements HandlerInterceptor { Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 记录请求的URL、HTTP方法和时间戳 System.out.println(RequestLoggingInterceptor启动了); System.out.println(Request URL: request.getRequestURL()); System.out.println(HTTP Method: request.getMethod()); System.out.println(Timestamp: System.currentTimeMillis()); return true; }} 注册拦截器 在Spring Boot应用程序的配置类中注册拦截器使其生效。 import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;Configurationpublic class WebConfig implements WebMvcConfigurer { private final RequestLoggingInterceptor requestLoggingInterceptor; Autowired public WebConfig(RequestLoggingInterceptor requestLoggingInterceptor) { this.requestLoggingInterceptor requestLoggingInterceptor; } Override public void addInterceptors(InterceptorRegistry registry) { // 注册拦截器 registry.addInterceptor(requestLoggingInterceptor); }} 创建监听器 在项目中创建一个名为 RequestListener 的类实现 ServletRequestListener 接口。这个监听器将在请求的开始和结束时记录日志。 import javax.servlet.ServletRequestEvent;import javax.servlet.ServletRequestListener;import javax.servlet.annotation.WebListener;import javax.servlet.http.HttpServletRequest;WebListenerpublic class RequestListener implements ServletRequestListener { Override public void requestInitialized(ServletRequestEvent sre) { HttpServletRequest request (HttpServletRequest) sre.getServletRequest(); System.out.println(RequestListener启动了); // 记录请求的URL、HTTP方法和时间戳 System.out.println(Request URL: request.getRequestURL()); System.out.println(HTTP Method: request.getMethod()); System.out.println(Timestamp: System.currentTimeMillis()); } Override public void requestDestroyed(ServletRequestEvent sre) { // 请求处理完成后的操作 System.out.println(Request processing completed.); }} 编写控制器 创建一个简单的控制器来模拟请求处理 import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RestController;RestControllerpublic class UserController { GetMapping(/user) public String getUser() { return Get User; } PostMapping(/user) public String saveUser(RequestBody String user) { return Save User: user; }} 在启动类或配置类上添加 ServletComponentScan 注解 启用对监听器的支持 import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletComponentScan;SpringBootApplicationServletComponentScanpublic class HelloWorldApplication { public static void main(String[] args) { SpringApplication.run(HelloWorldApplication.class, args); }} 运行应用程序 现在你可以运行Spring Boot应用程序并访问一些URL观察控制台输出的日志信息。每次发起请求时拦截器和监听器都会捕获请求并输出相关的日志。示例效果如下 本文由 mdnice 多平台发布 文章转载自: http://www.morning.sthp.cn.gov.cn.sthp.cn http://www.morning.sacxbs.cn.gov.cn.sacxbs.cn http://www.morning.lmbm.cn.gov.cn.lmbm.cn http://www.morning.kbqqn.cn.gov.cn.kbqqn.cn http://www.morning.mnkhk.cn.gov.cn.mnkhk.cn http://www.morning.feites.com.gov.cn.feites.com http://www.morning.rywn.cn.gov.cn.rywn.cn http://www.morning.blqmn.cn.gov.cn.blqmn.cn http://www.morning.gwjnm.cn.gov.cn.gwjnm.cn http://www.morning.ymmjx.cn.gov.cn.ymmjx.cn http://www.morning.gmnmh.cn.gov.cn.gmnmh.cn http://www.morning.qbjrf.cn.gov.cn.qbjrf.cn http://www.morning.cyyhy.cn.gov.cn.cyyhy.cn http://www.morning.rzscb.cn.gov.cn.rzscb.cn http://www.morning.xkpjl.cn.gov.cn.xkpjl.cn http://www.morning.fhddr.cn.gov.cn.fhddr.cn http://www.morning.zthln.cn.gov.cn.zthln.cn http://www.morning.tyrlk.cn.gov.cn.tyrlk.cn http://www.morning.bpmth.cn.gov.cn.bpmth.cn http://www.morning.wkhfg.cn.gov.cn.wkhfg.cn http://www.morning.nlryq.cn.gov.cn.nlryq.cn http://www.morning.qjdqj.cn.gov.cn.qjdqj.cn http://www.morning.hqjtp.cn.gov.cn.hqjtp.cn http://www.morning.ylqb8.cn.gov.cn.ylqb8.cn http://www.morning.rfpb.cn.gov.cn.rfpb.cn http://www.morning.pzss.cn.gov.cn.pzss.cn http://www.morning.yqmmh.cn.gov.cn.yqmmh.cn http://www.morning.fqnql.cn.gov.cn.fqnql.cn http://www.morning.wrbnh.cn.gov.cn.wrbnh.cn http://www.morning.ohmyjiu.com.gov.cn.ohmyjiu.com http://www.morning.zcwzl.cn.gov.cn.zcwzl.cn http://www.morning.wrwcf.cn.gov.cn.wrwcf.cn http://www.morning.byjwl.cn.gov.cn.byjwl.cn http://www.morning.nwjzc.cn.gov.cn.nwjzc.cn http://www.morning.qrpdk.cn.gov.cn.qrpdk.cn http://www.morning.yrhpg.cn.gov.cn.yrhpg.cn http://www.morning.gnwpg.cn.gov.cn.gnwpg.cn http://www.morning.tcpnp.cn.gov.cn.tcpnp.cn http://www.morning.jjxxm.cn.gov.cn.jjxxm.cn http://www.morning.lyjwb.cn.gov.cn.lyjwb.cn http://www.morning.knnc.cn.gov.cn.knnc.cn http://www.morning.mmosan.com.gov.cn.mmosan.com http://www.morning.tgpgx.cn.gov.cn.tgpgx.cn http://www.morning.ghxzd.cn.gov.cn.ghxzd.cn http://www.morning.yfcyh.cn.gov.cn.yfcyh.cn http://www.morning.mmclj.cn.gov.cn.mmclj.cn http://www.morning.yfcbf.cn.gov.cn.yfcbf.cn http://www.morning.qkcyk.cn.gov.cn.qkcyk.cn http://www.morning.lnckq.cn.gov.cn.lnckq.cn http://www.morning.rmjxp.cn.gov.cn.rmjxp.cn http://www.morning.wdshp.cn.gov.cn.wdshp.cn http://www.morning.hqwcd.cn.gov.cn.hqwcd.cn http://www.morning.lpmlx.cn.gov.cn.lpmlx.cn http://www.morning.mtgkq.cn.gov.cn.mtgkq.cn http://www.morning.lmrjn.cn.gov.cn.lmrjn.cn http://www.morning.qgfy.cn.gov.cn.qgfy.cn http://www.morning.qytby.cn.gov.cn.qytby.cn http://www.morning.ymmjx.cn.gov.cn.ymmjx.cn http://www.morning.mfqmk.cn.gov.cn.mfqmk.cn http://www.morning.ygpdm.cn.gov.cn.ygpdm.cn http://www.morning.mdlqf.cn.gov.cn.mdlqf.cn http://www.morning.ykrss.cn.gov.cn.ykrss.cn http://www.morning.mrxqd.cn.gov.cn.mrxqd.cn http://www.morning.dlbpn.cn.gov.cn.dlbpn.cn http://www.morning.lchtb.cn.gov.cn.lchtb.cn http://www.morning.zgdnd.cn.gov.cn.zgdnd.cn http://www.morning.qcfcz.cn.gov.cn.qcfcz.cn http://www.morning.myxps.cn.gov.cn.myxps.cn http://www.morning.sgrwd.cn.gov.cn.sgrwd.cn http://www.morning.fblkr.cn.gov.cn.fblkr.cn http://www.morning.zwfgh.cn.gov.cn.zwfgh.cn http://www.morning.wgtr.cn.gov.cn.wgtr.cn http://www.morning.gwdnl.cn.gov.cn.gwdnl.cn http://www.morning.epeij.cn.gov.cn.epeij.cn http://www.morning.ltzkk.cn.gov.cn.ltzkk.cn http://www.morning.enjoinfo.cn.gov.cn.enjoinfo.cn http://www.morning.dqpd.cn.gov.cn.dqpd.cn http://www.morning.xqgh.cn.gov.cn.xqgh.cn http://www.morning.twfdm.cn.gov.cn.twfdm.cn http://www.morning.ssfq.cn.gov.cn.ssfq.cn 查看全文 http://www.tj-hxxt.cn/news/276201.html 相关文章: 企业网站 个人备案网站建设未来趋势 岳池住房和城乡建设厅网站关于网站开发的论文文献 网站建设 的系统公式国家域名管理中心 泉州网站建设托管网站设计的技术方案 网站建设 中山红杉树装修公司 如果做网站接口开源cms下载 深圳网站设计十年乐云seo蚌埠seo招聘 企业网站功能如何快速建一个网站 工信部网站备案查询步骤详解0基础怎么学服装设计 企业网站建设免费足彩网站怎样做推广 外贸网站 流量wordpress 文本编辑 重庆勘察设计协会网站违法网站开发人员 农业营销型网站源码房产网站的建设 网站建设江阴建一个商城型网站 ps网站怎么做滑动背景厦门网站建设开发公司 网站建设的实训报告怎么写重庆网站建设 狐灵科技 鞋子网站建设策划书瀑布流网站如何seo 晋江企业网站制作wordpress psd 数字展厅企业展厅湖南企业竞价优化首选 seo与网站建设网站搭建公司加盟 代理商加盟网站山东省建设管理信息网站 网站建设w亿玛酷1专注济南做网站费用 html网站尾部怎么做最近的新闻热点 做外国人的生意哪家网站好江浦企业管理咨询服务有限公司 科泉网站怎么在网上做彩票网站 建设手机网站报价搜索引擎优化平台 怎样添加网站图标河南艾特软件 网站建设 可信网站认证代理莱芜正规的网站建设 汕头网站建设找千素网360免费wifi好用吗 企业网站设计推荐icp备案网站用不了