亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java攔截器沒有被調用

Java攔截器沒有被調用

白衣染霜花 2023-02-23 15:52:02
我有一個 Spring boot 應用程序,我正在實施和攔截器以記錄一些數據。問題是沒有被調用,我試過:@Interceptorpublic class LoggerInterceptor{    @AroundInvoke    public Object collectBasicLoggingInformation(InvocationContext context) throws Exception {        Logger logger = LoggerFactory.getLogger(context.getClass());        logger.info("Method Called: " + context.getMethod()                .getName());        logger.info("Parameters: " + Arrays.toString(context.getParameters()));        return context.proceed();    }}然后我已經應用于方法或類,但在它們中都不起作用:@GetMapping@Interceptors(LoggerInterceptor.class)public List getAllFilingNumber(){    logger.info("This is a test");    return filingNumberService.findAll();}或者@RestController@RequestMapping(FilingNumberController.BASE_URL)@Interceptors(LoggerInterceptor.class)public class FilingNumberController{    @GetMapping    public List getAllFilingNumber(){        logger.info("This is a test");        return filingNumberService.findAll();    }}有人知道我做錯了什么嗎?
查看完整描述

1 回答

?
拉丁的傳說

TA貢獻1789條經驗 獲得超8個贊

如果你有一個 springboot 應用程序來攔截對控制器的請求,你必須采取不同的方法。


攔截器與 Java EE 托管類結合使用,以允許開發人員在關聯的目標類上調用攔截器方法,并結合方法調用或生命周期事件。攔截器的常見用途是日志記錄、審計和分析。參考文檔


您正在嘗試將 Java EE 注釋與 spring 一起使用,這是行不通的。在 spring-boot 中,您必須注冊攔截器,例如:


@Configuration

public class WebConfig implements WebMvcConfigurer {


    @Override

    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(new LocaleChangeInterceptor());

        registry.addInterceptor(new ThemeChangeInterceptor()).addPathPatterns("/**").excludePathPatterns("/admin/**");

        registry.addInterceptor(new SecurityInterceptor()).addPathPatterns("/secure/*");

    }

}

攔截器本身必須是一個擴展HandlerInterceptorAdapter 并實現如下方法的類。來自 Spring文檔:


所有 HandlerMapping 實現都支持處理程序攔截器,當您想要將特定功能應用于某些請求時,這些攔截器很有用——例如,檢查主體。攔截器必須使用 org.springframework.web.servlet 包中的三個方法實現 HandlerInterceptor,這三個方法應該提供足夠的靈活性來進行各種預處理和后處理:


preHandle(..): Before the actual handler is executed


postHandle(..): After the handler is executed


afterCompletion(..): After the complete request has finished

@Component

public class RequestInterceptor extends HandlerInterceptorAdapter {


 @Override

 public boolean preHandle(HttpServletRequest request, 

        HttpServletResponse response, Object object) throws Exception {

    System.out.println("we are Intercepting the Request");

    return true;

 }


 @Override

 public void postHandle(HttpServletRequest request, HttpServletResponse response, 

        Object object, ModelAndView model)

        throws Exception {

    System.out.println("request processing "

            + "completed by @RestController");


 }


 @Override

 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, 

        Object object, Exception arg3)

        throws Exception {

    System.out.println("afterCompletion Request Completed");

 }

}


查看完整回答
反對 回復 2023-02-23
  • 1 回答
  • 0 關注
  • 164 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號