Use SpringMVC Interceptor to control Controller return values
Background: when the method in the Controller is not implemented, the simulation result is returned. It is mainly used for front-end interaction with the background at the initial stage of the project. A Web project sends a request at the front-end and then responds to the background and returns the result. In this example, the interceptor and annotation are used to skip the execution method and directly return the definition structure.
Define a StringResult annotation to return the content in StringResult when accessing the method. Use the Debug annotation to determine whether the method will return content in StringResult.
The default value of Debug is 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)@Documentedpublic @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)@Documentedpublic @interface StringResult {String value();}Define the annotation and write the interceptor class. The interceptor must implement 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. stringResul T; public class DebugInterceprot implements HandlerInterceptor {private boolean debug = true; public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {// first determine whether the Debug mode (global) is used. Otherwise, the interceptor fails 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); // skip the interception if there is no @ StringResult annotation. // determine the Debug value of the Annotation on the method. if otherwise, if (stringResult = null | (isDebug! = Null & isDebug. value () = false) {return true;} else {// intercept the method and return the content in stringResult to 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 configuration
Controller statement
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!";}}This method can be used to test the foreground functions when the methods in the controller are not well written. The idea is like this. More powerful functions need to be developed by the experts. This is just an unexpected thought, and I have not actually tried it in the project. If someone has tried the experiment in the project, please let me know the results. Thank you.
If someone uses the StringResult annotation, we recommend that you keep the StringResult annotation, because this annotation will let you know what kind of result your method will return.
Agility is a long way to go !!