详解利用SpringMVC拦截器控制Controller返回值

所属分类: 软件编程 / java 阅读数: 43
收藏 0 赞 0 分享

背景:需求是在Controller中方法没有实现时,返回模拟结果。主要用于项目初期前台跟后台的交互,Web项目就是在前台发出请求然后后台响应并返回结果。本示例利用拦截器和注解实现跳过执行方法直接返回定义结构的功能。

通过定义一个StringResult注解,在访问方法的时候返回StringResult中的内容。通过Debug注解来定义方法是否要返回StringResult中的内容。

Debug默认为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(); 
} 

定义好注解之后写拦截器类,拦截器需要实现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); 
      //如果没有@StringResult注解则跳过拦截 
      //判断方法上注解的Debug值,如果否则不拦截 
      if(stringResult==null||(isDebug !=null && isDebug.value() == false)){ 
        return true; 
      }else{ 
        //拦截方法,并将stringResult中的内容返回给前台 
        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!"; 
  } 
} 

此方法可用以在控制器中的方法没有写好的时候进行前台功能的测试,思路大概如此,更加强大的功能需要各位大神们开发。这个只是我的突发奇想,并没有实际在项目中试过。如果有人在项目中试了请告诉我效果,谢谢。

如果有人用了,建议保留StringResult注解,因为这个注解可以让你知道你的方法要返回一个什么样的结果。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多