詳解利用SpringMVC攔截器控制Controller返回值
背景:需求是在Controller中方法沒(méi)有實(shí)現(xiàn)時(shí),返回模擬結(jié)果。主要用于項(xiàng)目初期前臺(tái)跟后臺(tái)的交互,Web項(xiàng)目就是在前臺(tái)發(fā)出請(qǐng)求然后后臺(tái)響應(yīng)并返回結(jié)果。本示例利用攔截器和注解實(shí)現(xiàn)跳過(guò)執(zhí)行方法直接返回定義結(jié)構(gòu)的功能。
通過(guò)定義一個(gè)StringResult注解,在訪問(wèn)方法的時(shí)候返回StringResult中的內(nèi)容。通過(guò)Debug注解來(lái)定義方法是否要返回StringResult中的內(nèi)容。
Debug默認(rèn)為TRUE
package com.tiamaes.dep.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Debug { boolean value() default true; }
package com.tiamaes.dep.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface StringResult { String value(); }
定義好注解之后寫攔截器類,攔截器需要實(shí)現(xiàn)HandlerInterceptor
package com.tiamaes.dep.interceptor; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import com.tiamaes.dep.annotation.Debug; import com.tiamaes.dep.annotation.StringResult; public class DebugInterceprot implements HandlerInterceptor { private boolean debug = true; public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //首先判斷是否是Debug模式(全局),如果否則使攔截器失效 if(!this.debug) return true; if(handler instanceof HandlerMethod){ HandlerMethod method = (HandlerMethod)handler; Debug isDebug = method.getMethodAnnotation(Debug.class); StringResult stringResult = method.getMethodAnnotation(StringResult.class); //如果沒(méi)有@StringResult注解則跳過(guò)攔截 //判斷方法上注解的Debug值,如果否則不攔截 if(stringResult==null||(isDebug !=null && isDebug.value() == false)){ return true; }else{ //攔截方法,并將stringResult中的內(nèi)容返回給前臺(tái) PrintWriter out = response.getWriter(); out.print(stringResult.value()); } } return false; } public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // TODO Auto-generated method stub } public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // TODO Auto-generated method stub } public boolean isDebug() { return debug; } public void setDebug(boolean debug) { this.debug = debug; } }
XML配置
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="com.tiamaes.dep.interceptor.DebugInterceprot"> <property name="debug" value="true"/> </bean> </mvc:interceptor> </mvc:interceptors>
Controller中的寫法
package com.tiamaes.dep.system.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.tiamaes.dep.annotation.Debug; import com.tiamaes.dep.annotation.StringResult; @Controller @RequestMapping("/test") public class AspectTestController { @RequestMapping("/1") @ResponseBody //@Debug(false) @StringResult("Interceptor") public String test1(){ return "The controller request!"; } }
此方法可用以在控制器中的方法沒(méi)有寫好的時(shí)候進(jìn)行前臺(tái)功能的測(cè)試,思路大概如此,更加強(qiáng)大的功能需要各位大神們開(kāi)發(fā)。這個(gè)只是我的突發(fā)奇想,并沒(méi)有實(shí)際在項(xiàng)目中試過(guò)。如果有人在項(xiàng)目中試了請(qǐng)告訴我效果,謝謝。
如果有人用了,建議保留StringResult注解,因?yàn)檫@個(gè)注解可以讓你知道你的方法要返回一個(gè)什么樣的結(jié)果。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java動(dòng)態(tài)規(guī)劃之編輯距離問(wèn)題示例代碼
這篇文章主要介紹了Java動(dòng)態(tài)規(guī)劃之編輯距離問(wèn)題示例代碼,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11如何使用Sentry 監(jiān)控你的Spring Boot應(yīng)用
這篇文章主要介紹了如何使用Sentry 監(jiān)控你的Spring Boot應(yīng)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Ajax實(shí)現(xiàn)搜索引擎自動(dòng)補(bǔ)全功能
本文主要介紹了Ajax實(shí)現(xiàn)搜索引擎自動(dòng)補(bǔ)全功能的實(shí)例解析。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-04-04使用java寫的矩陣乘法實(shí)例(Strassen算法)
這篇文章主要給大家介紹了關(guān)于如何使用java寫的矩陣乘法(Strassen算法)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02SpringCloud使用Zookeeper作為配置中心的示例
這篇文章主要介紹了SpringCloud使用Zookeeper作為配置中心的示例,幫助大家更好的理解和學(xué)習(xí)使用SpringCloud,感興趣的朋友可以了解下2021-04-04淺談SpringBoot之開(kāi)啟數(shù)據(jù)庫(kù)遷移的FlyWay使用
這篇文章主要介紹了淺談SpringBoot之開(kāi)啟數(shù)據(jù)庫(kù)遷移的FlyWay使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01