Spring?Boot攔截器和監(jiān)聽(tīng)器實(shí)現(xiàn)對(duì)請(qǐng)求和響應(yīng)處理實(shí)戰(zhàn)
引言
當(dāng)使用Spring Boot時(shí),我們可以通過(guò)攔截器(Interceptor)和監(jiān)聽(tīng)器(Listener)來(lái)實(shí)現(xiàn)對(duì)請(qǐng)求和響應(yīng)的處理。攔截器和監(jiān)聽(tīng)器提供了一種可插拔的機(jī)制,用于在請(qǐng)求處理過(guò)程中進(jìn)行自定義操作,例如記錄日志、身份驗(yàn)證、權(quán)限檢查等。下面通過(guò)提供一個(gè)示例,展示如何使用攔截器和監(jiān)聽(tīng)器來(lái)記錄請(qǐng)求日志。
首先,我們創(chuàng)建一個(gè)簡(jiǎn)單的Spring Boot項(xiàng)目,并添加所需的依賴。在這個(gè)示例中,我們將使用Spring Boot Starter Web。
創(chuàng)建一個(gè)Spring Boot項(xiàng)目并添加依賴
創(chuàng)建一個(gè)新的Spring Boot項(xiàng)目,可以使用Spring Initializr(https://start.spring.io/)進(jìn)行初始化。
在"Dependencies"中添加"Spring Web"依賴,并生成項(xiàng)目。
創(chuàng)建攔截器
在項(xiàng)目中創(chuàng)建一個(gè)名為 RequestLoggingInterceptor 的類(lèi),實(shí)現(xiàn) HandlerInterceptor 接口。這個(gè)攔截器將記錄請(qǐng)求的URL、HTTP方法和時(shí)間戳。
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 {
// 記錄請(qǐng)求的URL、HTTP方法和時(shí)間戳
System.out.println("RequestLoggingInterceptor"+"啟動(dòng)了");
System.out.println("Request URL: " + request.getRequestURL());
System.out.println("HTTP Method: " + request.getMethod());
System.out.println("Timestamp: " + System.currentTimeMillis());
return true;
}
}注冊(cè)攔截器
在Spring Boot應(yīng)用程序的配置類(lèi)中,注冊(cè)攔截器,使其生效。
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) {
// 注冊(cè)攔截器
registry.addInterceptor(requestLoggingInterceptor);
}
}創(chuàng)建監(jiān)聽(tīng)器
在項(xiàng)目中創(chuàng)建一個(gè)名為 RequestListener 的類(lèi),實(shí)現(xiàn) ServletRequestListener 接口。這個(gè)監(jiān)聽(tīng)器將在請(qǐng)求的開(kāi)始和結(jié)束時(shí)記錄日志。
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"+"啟動(dòng)了");
// 記錄請(qǐng)求的URL、HTTP方法和時(shí)間戳
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) {
// 請(qǐng)求處理完成后的操作
System.out.println("Request processing completed.");
}
}編寫(xiě)控制器
創(chuàng)建一個(gè)簡(jiǎn)單的控制器來(lái)模擬請(qǐ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;
}
}- 在啟動(dòng)類(lèi)或配置類(lèi)上添加 @ServletComponentScan 注解
啟用對(duì)監(jiān)聽(tīng)器的支持
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)用程序并訪問(wèn)一些URL,觀察控制臺(tái)輸出的日志信息。每次發(fā)起請(qǐng)求時(shí),攔截器和監(jiān)聽(tīng)器都會(huì)捕獲請(qǐng)求并輸出相關(guān)的日志。示例效果如下:

以上就是Spring Boot攔截器和監(jiān)聽(tīng)器應(yīng)用實(shí)戰(zhàn)指南的詳細(xì)內(nèi)容,更多關(guān)于Spring Boot攔截器監(jiān)聽(tīng)器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java?freemarker實(shí)現(xiàn)動(dòng)態(tài)生成excel文件
這篇文章主要為大家詳細(xì)介紹了java如何通過(guò)freemarker實(shí)現(xiàn)動(dòng)態(tài)生成excel文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12
一文快速了解spring?boot中的@idempotent注解
idempotence注解是RESTful API設(shè)計(jì)中一個(gè)重要的概念,它可以保證操作的可靠性和一致性,下面這篇文章主要給大家介紹了關(guān)于spring?boot中@idempotent注解的相關(guān)資料,需要的朋友可以參考下2024-01-01
SpringMVC+MyBatis 事務(wù)管理(實(shí)例)
本文先分析編程式注解事務(wù)和基于注解的聲明式事務(wù)。對(duì)SpringMVC+MyBatis 事務(wù)管理的相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2017-08-08
MyBatis參數(shù)綁定中參數(shù)名不一致導(dǎo)致的錯(cuò)誤問(wèn)題解決方法
作為一名Java開(kāi)發(fā)者,我在實(shí)際項(xiàng)目中曾多次遇到MyBatis參數(shù)綁定的問(wèn)題,其中最常見(jiàn)的一種情況是MyBatis參數(shù)綁定中參數(shù)名不一致導(dǎo)致的錯(cuò)誤,這類(lèi)問(wèn)題看似簡(jiǎn)單,但若不深入理解MyBatis的參數(shù)綁定機(jī)制,極易陷入誤區(qū),本文將帶大家一起探討解決方案,需要的朋友可以參考下2025-06-06
NoHttpResponseException問(wèn)題排查解決記錄分析
這篇文章主要為大家介紹了NoHttpResponseException問(wèn)題排查解決記錄分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
java中 String和StringBuffer的區(qū)別實(shí)例詳解
這篇文章主要介紹了java中 String和StringBuffer的區(qū)別實(shí)例詳解的相關(guān)資料,一個(gè)小的例子,來(lái)測(cè)試String和StringBuffer在時(shí)間和空間使用上的差別,需要的朋友可以參考下2017-04-04
MyEclipse打開(kāi)文件跳轉(zhuǎn)到notepad打開(kāi)問(wèn)題及解決方案
windows系統(tǒng)打開(kāi)README.md文件,每次都需要右鍵選擇notepad打開(kāi),感覺(jué)很麻煩,然后就把README.md文件打開(kāi)方式默認(rèn)選擇了notepad,這樣每次雙擊就能打開(kāi),感覺(jué)很方便,這篇文章主要介紹了MyEclipse打開(kāi)文件跳轉(zhuǎn)到notepad打開(kāi)問(wèn)題,需要的朋友可以參考下2024-03-03
Eclipse啟動(dòng)Tomcat超時(shí)問(wèn)題的解決方法
2013-03-03

