欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring?Boot攔截器和監(jiān)聽器實現(xiàn)對請求和響應(yīng)處理實戰(zhàn)

 更新時間:2023年06月25日 10:13:41   作者:劉鳳貴  
這篇文章主要介紹了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)文章

  • Java 值傳遞和引用傳遞詳解及實例代碼

    Java 值傳遞和引用傳遞詳解及實例代碼

    這篇文章主要介紹了 Java 值傳遞和引用傳遞詳解及實例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Elasticsearch寫入瓶頸導(dǎo)致skywalking大盤空白

    Elasticsearch寫入瓶頸導(dǎo)致skywalking大盤空白

    這篇文章主要為大家介紹了Elasticsearch寫入瓶頸導(dǎo)致skywalking大盤空白的解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2022-02-02
  • Java實現(xiàn)JSP在Servelt中連接Oracle數(shù)據(jù)庫的方法

    Java實現(xiàn)JSP在Servelt中連接Oracle數(shù)據(jù)庫的方法

    這篇文章主要介紹了Java實現(xiàn)JSP在Servelt中連接Oracle數(shù)據(jù)庫的方法,需要的朋友可以參考下
    2014-07-07
  • idea運行java項目main方法報build failure錯誤的解決方法

    idea運行java項目main方法報build failure錯誤的解決方法

    當(dāng)在使用 IntelliJ IDEA 運行 Java 項目的 main 方法時遇到 "Build Failure" 錯誤,這通常意味著在項目的構(gòu)建過程中遇到了問題,以下是一些詳細(xì)的解決步驟,以及一個簡單的代碼示例,用于展示如何確保 Java 程序可以成功構(gòu)建和運行,需要的朋友可以參考下
    2024-09-09
  • 詳解Java設(shè)計模式中的裝飾模式

    詳解Java設(shè)計模式中的裝飾模式

    裝飾模式是指在不改變現(xiàn)有對象結(jié)構(gòu)的情況下,動態(tài)地給該對象增加一些職責(zé)(即增加其額外功能)的模式,它屬于對象結(jié)構(gòu)型模式。本文將為大家詳細(xì)介紹一下裝飾模式,感興趣的可以了解一下
    2021-12-12
  • 基于Spring Boot 排除自動配置的4個方法

    基于Spring Boot 排除自動配置的4個方法

    這篇文章主要介紹了Spring Boot 排除自動配置的4個方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java8方法引用和構(gòu)造引用代碼實例

    Java8方法引用和構(gòu)造引用代碼實例

    這篇文章主要介紹了java8方法引用和構(gòu)造引用代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • idea項目實現(xiàn)移除和添加git

    idea項目實現(xiàn)移除和添加git

    本文指導(dǎo)讀者如何從官網(wǎng)下載并安裝Git,以及在IDEA中配置Git的詳細(xì)步驟,首先,用戶需訪問Git官方網(wǎng)站下載適合自己操作系統(tǒng)的Git版本并完成安裝,接著,在IDEA中通過設(shè)置找到git.exe文件以配置Gi
    2024-10-10
  • 詳解Redisson分布式限流的使用及原理

    詳解Redisson分布式限流的使用及原理

    本文介紹了Redisson分布式限流的使用方法和原理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-02-02
  • java實現(xiàn)把對象數(shù)組通過excel方式導(dǎo)出的功能

    java實現(xiàn)把對象數(shù)組通過excel方式導(dǎo)出的功能

    本文主要介紹了java實現(xiàn)把對象數(shù)組通過excel方式導(dǎo)出的功能的相關(guān)知識。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-03-03

最新評論