Detail USES the SpringMVC interceptor to control the Controller return value

  • 2020-06-01 09:44:42
  • OfStack

Background: the requirement is to return simulation results if the method is not implemented in Controller. It is mainly used for the interaction between the foreground and the background in the early stage of the project. The Web project is to issue a request in the foreground and then respond in the background and return the result. This example USES interceptors and annotations to implement the ability to skip execution methods and return the defined structure directly.

Returns the contents of StringResult when accessing the method by defining an StringResult annotation. Use the Debug annotation to define whether the method should return content in StringResult.

Debug defaults to 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(); 
} 

After defining the annotations, write the interceptor class, which needs to 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.StringResult; 
 
public class DebugInterceprot implements HandlerInterceptor { 
  private boolean debug = true; 
   
  public boolean preHandle(HttpServletRequest request, 
      HttpServletResponse response, Object handler) throws Exception { 
    // First determine if Debug model ( global ) , otherwise disable the interceptor  
    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); 
      // If there is no @StringResult Annotations skip interception  
      // Annotated on the judgment method Debug Value if otherwise not intercepted  
      if(stringResult==null||(isDebug !=null && isDebug.value() == false)){ 
        return true; 
      }else{ 
        // Intercept the method and will stringResult Is returned to the foreground  
        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


<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!"; 
  } 
} 

This method can be used to test the foreground function when the method in the controller is not written well. The idea is roughly like this. More powerful functions need to be developed by the gods. This is just a whim of mine, and I haven't actually tried it in the project. Please let me know if anyone has tried it in the project. Thank you.

If someone does, it is recommended to keep the StringResult annotation, because this annotation will let you know what result your method will return.


Related articles: