當(dāng)使用Spring Boot時,我們可以通過攔截器(Interceptor)和監(jiān)聽器(Listener)來實現(xiàn)對請求和響應(yīng)的處理。攔截器和監(jiān)聽器提供了一種可插拔的機(jī)制,用于在請求處理過程中進(jìn)行自定義操作,例如記錄日志、身份驗證、權(quán)限檢查等。下面通過提供一個示例,展示如何使用攔截器和監(jiān)聽器來記錄請求日志。
首先,我們創(chuàng)建一個簡單的Spring Boot項目,并添加所需的依賴。在這個示例中,我們將使用Spring Boot Starter Web。
-
創(chuàng)建一個Spring Boot項目并添加依賴
創(chuàng)建一個新的Spring Boot項目,可以使用Spring Initializr(https://start.spring.io/)進(jìn)行初始化。在"Dependencies"中添加"Spring Web"依賴,并生成項目。
-
創(chuàng)建攔截器
在項目中創(chuàng)建一個名為 RequestLoggingInterceptor
的類,實現(xiàn) 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應(yīng)用程序的配置類中,注冊攔截器,使其生效。
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;
@Configuration
public?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);
????}
}
-
創(chuàng)建監(jiān)聽器
在項目中創(chuàng)建一個名為 RequestListener
的類,實現(xiàn) ServletRequestListener
接口。這個監(jiān)聽器將在請求的開始和結(jié)束時記錄日志。
import?javax.servlet.ServletRequestEvent;
import?javax.servlet.ServletRequestListener;
import?javax.servlet.annotation.WebListener;
import?javax.servlet.http.HttpServletRequest;
@WebListener
public?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.");
????}
}
-
編寫控制器
創(chuàng)建一個簡單的控制器來模擬請求處理
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;
@RestController
public?class?UserController?{
????@GetMapping("/user")
????public?String?getUser()?{
????????return?"Get?User";
????}
????@PostMapping("/user")
????public?String?saveUser(@RequestBody?String?user)?{
????????return?"Save?User:?"?+?user;
????}
}
-
在啟動類或配置類上添加 @ServletComponentScan 注解
啟用對監(jiān)聽器的支持
import?org.springframework.boot.SpringApplication;
import?org.springframework.boot.autoconfigure.SpringBootApplication;
import?org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan
public?class?HelloWorldApplication?{
????public?static?void?main(String[]?args)?{
????????SpringApplication.run(HelloWorldApplication.class,?args);
????}
}
-
運(yùn)行應(yīng)用程序
現(xiàn)在,你可以運(yùn)行Spring Boot應(yīng)用程序并訪問一些URL,觀察控制臺輸出的日志信息。每次發(fā)起請求時,攔截器和監(jiān)聽器都會捕獲請求并輸出相關(guān)的日志。示例效果如下:文章來源:http://www.zghlxwxcb.cn/news/detail-491496.html

本文由 mdnice 多平臺發(fā)布文章來源地址http://www.zghlxwxcb.cn/news/detail-491496.html
到了這里,關(guān)于Spring Boot實戰(zhàn):攔截器和監(jiān)聽器的應(yīng)用指南的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!