Spring?Boot攔截器和監(jiān)聽器實(shí)現(xiàn)對(duì)請(qǐng)求和響應(yīng)處理實(shí)戰(zhàn)
引言
當(dāng)使用Spring Boot時(shí),我們可以通過(guò)攔截器(Interceptor)和監(jiān)聽器(Listener)來(lái)實(shí)現(xiàn)對(duì)請(qǐng)求和響應(yīng)的處理。攔截器和監(jiān)聽器提供了一種可插拔的機(jī)制,用于在請(qǐng)求處理過(guò)程中進(jìn)行自定義操作,例如記錄日志、身份驗(yàn)證、權(quán)限檢查等。下面通過(guò)提供一個(gè)示例,展示如何使用攔截器和監(jiān)聽器來(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 的類,實(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)用程序的配置類中,注冊(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)聽器
在項(xiàng)目中創(chuàng)建一個(gè)名為 RequestListener 的類,實(shí)現(xiàn) ServletRequestListener 接口。這個(gè)監(jiān)聽器將在請(qǐng)求的開始和結(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.");
}
}編寫控制器
創(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)類或配置類上添加 @ServletComponentScan 注解
啟用對(duì)監(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)用程序并訪問(wèn)一些URL,觀察控制臺(tái)輸出的日志信息。每次發(fā)起請(qǐng)求時(shí),攔截器和監(jiān)聽器都會(huì)捕獲請(qǐng)求并輸出相關(guān)的日志。示例效果如下:

以上就是Spring Boot攔截器和監(jiān)聽器應(yīng)用實(shí)戰(zhàn)指南的詳細(xì)內(nèi)容,更多關(guān)于Spring Boot攔截器監(jiān)聽器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Elasticsearch寫入瓶頸導(dǎo)致skywalking大盤空白
這篇文章主要為大家介紹了Elasticsearch寫入瓶頸導(dǎo)致skywalking大盤空白的解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02
Java實(shí)現(xiàn)JSP在Servelt中連接Oracle數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了Java實(shí)現(xiàn)JSP在Servelt中連接Oracle數(shù)據(jù)庫(kù)的方法,需要的朋友可以參考下2014-07-07
idea運(yùn)行java項(xiàng)目main方法報(bào)build failure錯(cuò)誤的解決方法
當(dāng)在使用 IntelliJ IDEA 運(yùn)行 Java 項(xiàng)目的 main 方法時(shí)遇到 "Build Failure" 錯(cuò)誤,這通常意味著在項(xiàng)目的構(gòu)建過(guò)程中遇到了問(wèn)題,以下是一些詳細(xì)的解決步驟,以及一個(gè)簡(jiǎn)單的代碼示例,用于展示如何確保 Java 程序可以成功構(gòu)建和運(yùn)行,需要的朋友可以參考下2024-09-09
基于Spring Boot 排除自動(dòng)配置的4個(gè)方法
這篇文章主要介紹了Spring Boot 排除自動(dòng)配置的4個(gè)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
idea項(xiàng)目實(shí)現(xiàn)移除和添加git
本文指導(dǎo)讀者如何從官網(wǎng)下載并安裝Git,以及在IDEA中配置Git的詳細(xì)步驟,首先,用戶需訪問(wèn)Git官方網(wǎng)站下載適合自己操作系統(tǒng)的Git版本并完成安裝,接著,在IDEA中通過(guò)設(shè)置找到git.exe文件以配置Gi2024-10-10
java實(shí)現(xiàn)把對(duì)象數(shù)組通過(guò)excel方式導(dǎo)出的功能
本文主要介紹了java實(shí)現(xiàn)把對(duì)象數(shù)組通過(guò)excel方式導(dǎo)出的功能的相關(guān)知識(shí)。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-03-03

