Spring?Boot攔截器和監(jiān)聽器實現(xiàn)對請求和響應(yīng)處理實戰(zhàn)
引言
當(dāng)使用Spring Boot時,我們可以通過攔截器(Interceptor)和監(jiān)聽器(Listener)來實現(xiàn)對請求和響應(yīng)的處理。攔截器和監(jiā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/)進行初始化。
在"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īng)用程序
現(xiàn)在,你可以運行Spring Boot應(yīng)用程序并訪問一些URL,觀察控制臺輸出的日志信息。每次發(fā)起請求時,攔截器和監(jiān)聽器都會捕獲請求并輸出相關(guān)的日志。示例效果如下:
以上就是Spring Boot攔截器和監(jiān)聽器應(yīng)用實戰(zhàn)指南的詳細(xì)內(nèi)容,更多關(guān)于Spring Boot攔截器監(jiān)聽器的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Elasticsearch寫入瓶頸導(dǎo)致skywalking大盤空白
這篇文章主要為大家介紹了Elasticsearch寫入瓶頸導(dǎo)致skywalking大盤空白的解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-02-02Java實現(xiàn)JSP在Servelt中連接Oracle數(shù)據(jù)庫的方法
這篇文章主要介紹了Java實現(xiàn)JSP在Servelt中連接Oracle數(shù)據(jù)庫的方法,需要的朋友可以參考下2014-07-07idea運行java項目main方法報build failure錯誤的解決方法
當(dāng)在使用 IntelliJ IDEA 運行 Java 項目的 main 方法時遇到 "Build Failure" 錯誤,這通常意味著在項目的構(gòu)建過程中遇到了問題,以下是一些詳細(xì)的解決步驟,以及一個簡單的代碼示例,用于展示如何確保 Java 程序可以成功構(gòu)建和運行,需要的朋友可以參考下2024-09-09java實現(xiàn)把對象數(shù)組通過excel方式導(dǎo)出的功能
本文主要介紹了java實現(xiàn)把對象數(shù)組通過excel方式導(dǎo)出的功能的相關(guān)知識。具有很好的參考價值,下面跟著小編一起來看下吧2017-03-03