最近在開發專案介面,基於SpringBoot 2.6.8,最終部署到外接Tomcat 8.5.85 下,開發過程中寫了一個CookieFilter,實現javax.servlet.Filter介面,程式碼編譯期正常。部署到外接Tomcat 8.5.85 下,在控制檯上報錯:
16-Jan-2023 16:11:07.756 嚴重 [localhost-startStop-1] org.apache.catalina.core.StandardContext.filterStart 啟動過濾器異常[cookieFilter]
java.lang.AbstractMethodError
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:281)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:109)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4604)
...省略其他輸出...
紀錄檔截圖如下:
除了初始化錯誤還有銷燬錯誤,錯誤型別與以下的錯誤型別一致:
16-Jan-2023 16:11:07.876 嚴重 [localhost-startStop-1] org.apache.catalina.core.ApplicationFilterConfig.release 失敗的銷燬過濾器型別為[xx.CookieFilter]名稱為[CookieFilter]
java.lang.AbstractMethodError
at org.apache.catalina.core.ApplicationFilterConfig.release(ApplicationFilterConfig.java:312)
at org.apache.catalina.core.StandardContext.filterStop(StandardContext.java:4638)
...省略其他輸出...
紀錄檔截圖如下:
我的程式碼差不多長這樣:
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class CookieFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
//做一些處理...
filterChain.doFilter(request, response);
}
}
javax.servlet:javax.servlet-api
依賴包,這個包裡定義的Filter介面的init()與destroy()是有預設實現的,程式碼如圖:這幾種方案中對於研發層面最簡單避免這個問題的就是方案一,這裡的解決方案是拋磚引玉,歡迎大家評論給出更優解。我是Hellxz,下次部落格見。