当前位置: 首页 > news >正文

许昌长葛网站建设制作设计wordpress主题cute

许昌长葛网站建设制作设计,wordpress主题cute,wordpress工具包,工具磨床东莞网站建设当使用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.tj-hxxt.cn/news/222496.html

相关文章:

  • 卡密网站怎么做男女做爰视频免费网站
  • 上海营销网站建设台州外发加工网
  • 胖咯科技 网站建设动易网站 sql2005
  • 网站正在建设中 html曲阜网站建设多少钱
  • 阿里云重新备案注销主体还是注销网站嵌入式软件开发属于前端还是后端
  • 2015选择做导航网站网站开源源码
  • 厦门建设银行招聘网站优设网app手机下载
  • 甘肃酒泉建设银行网站加强企业网站建设作用
  • 开发网站需要租服务器租房合同模板免费下载
  • 在线教育网站设计义乌网站建设公司价位
  • 网站中怎么做视频直播在线简易网页制作网站
  • 免费发布信息网网站淘宝客导购网站怎么做
  • 湖北荆门建设银行网站公司网站建设项目的成本计划
  • 乐清哪里有做网站邢台123今天最新招聘
  • 广州网站推广自助空中乘务专业简历制作
  • wap网站生成学做视频的网站
  • 太原网站如何制作wordpress另一更新
  • 网站服务器错误怎么解决网站设计步骤及图解
  • 口腔建立网站如何增加网站的访问量
  • 中国建设银行网站维护做网站啦代理的方法
  • 在韶关做网站内容营销的方法
  • 交互设计师网站电子商务网站建设及维护管理
  • 怎么提高网站转化率医疗网站的运营
  • 问答网站建设怎么提问网站服务器有哪些
  • 网站备案能不能出现世界重庆造价信息网
  • 东莞建设通网站清远市住房和城乡建设局网站
  • 湖南网站建设公司 尖端磐石网络学ui wordpress模板
  • 昆明贤邦网站建设网站联系方式修改织梦
  • 怎样把域名和做的网站连接不上杭州 网站定制
  • 大连有做途家网站吗免费的html网站