SpringBoot配置攔截器的示例
在SpringBoot中配置攔截器,主要有下面兩個步驟:
1、繼承接口 HandlerInterceptor,根據(jù)需要重寫其中的三個類。
2、在配置類中注入該類。
public class MyInterceptor implements HandlerInterceptor { //controller執(zhí)行之前 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("preHandler......"); return true; } //執(zhí)行完controller執(zhí)行之后、視圖渲染前調(diào)用,可以在該方法里獲取或者修改model @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("postHandler......"); } //一般用于清理資源 @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("afterCompletion......"); } }
@Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { //1、全部攔截 // registry.addInterceptor(myInterceptor()).addPathPatterns("/**"); //2、攔截指定路徑 registry.addInterceptor(myInterceptor()).addPathPatterns("/hello"); } @Bean MyInterceptor myInterceptor(){ return new MyInterceptor(); } }
寫個controller測試一下
@RestController public class HelloController { @RequestMapping("/hello") public String hello(){ System.out.println("hello"); return "hello"; } @RequestMapping("/world") public String world(){ System.out.println("world"); return "world"; } }
測試結(jié)果:
preHandler......
hello
postHandler......
afterCompletion......
world
SpringBoot中還有一終攔截器,WebRequestInterceptor
public class MyWebRequestInterceptor implements WebRequestInterceptor { @Override public void preHandle(WebRequest webRequest) throws Exception { } @Override public void postHandle(WebRequest webRequest, ModelMap modelMap) throws Exception { } @Override public void afterCompletion(WebRequest webRequest, Exception e) throws Exception { } }
和HandlerInterceptor比較相似,但是可以發(fā)現(xiàn),該攔截器的preHandler返回值為空,說明該方法并不影響后面方法的執(zhí)行。那么這個攔截器存在的目的是什么吶?
點進WebRequest:
public interface WebRequest extends RequestAttributes { @Nullable String getHeader(String var1); @Nullable String[] getHeaderValues(String var1); Iterator<String> getHeaderNames(); @Nullable String getParameter(String var1); @Nullable String[] getParameterValues(String var1); Iterator<String> getParameterNames(); Map<String, String[]> getParameterMap(); Locale getLocale(); String getContextPath(); @Nullable String getRemoteUser(); @Nullable Principal getUserPrincipal(); boolean isUserInRole(String var1); boolean isSecure();
發(fā)現(xiàn)對reques請求中參數(shù)做了進一步處理(@Nullable表示可以為空),更加的方便調(diào)用。所以兩個攔截器的側(cè)重點不同,HandlerInterceptor功能較為強大,可以攔截請求,可以實現(xiàn)WebRequestInterceptor的所有功能,只是要寫的邏輯代碼要多一點。更而WebRequestInterceptor傾向于簡化獲取request參數(shù)的過程以及預設參數(shù)供后面的流程使用。
以上就是SpringBoot配置攔截器的示例的詳細內(nèi)容,更多關于SpringBoot配置攔截器的資料請關注腳本之家其它相關文章!
相關文章
SpringBoot執(zhí)行定時任務@Scheduled的方法
這篇文章主要介紹了SpringBoot執(zhí)行定時任務@Scheduled的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07Springboot?中的?Filter?實現(xiàn)超大響應?JSON?數(shù)據(jù)壓縮的方法
這篇文章主要介紹了Springboot?中的?Filter?實現(xiàn)超大響應?JSON?數(shù)據(jù)壓縮,定義GzipFilter對輸出進行攔截,定義 Controller該 Controller 非常簡單,主要讀取一個大文本文件,作為輸出的內(nèi)容,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-10-10Java畢業(yè)設計實戰(zhàn)之線上水果超市商城的實現(xiàn)
這是一個使用了java+SSM+springboot+redis開發(fā)的網(wǎng)上水果超市商城,是一個畢業(yè)設計的實戰(zhàn)練習,具有水果超市商城該有的所有功能,感興趣的朋友快來看看吧2022-01-01java servlet結(jié)合Oracle搭建java的web開發(fā)環(huán)境
今天我將與大家分享一下我學JAVA WEB寫的一些小實例 ,我個人是不太喜歡書本上的晦澀的概念的,所以我花了更多的時間在一些應用實例上,我覺得這樣的學習方式很適合我,由簡到繁,由淺入深2015-12-12