当前位置: 移动技术网 > IT编程>开发语言>Java > 详解利用SpringMVC拦截器控制Controller返回值

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

背景:需求是在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注解,因为这个注解可以让你知道你的方法要返回一个什么样的结果。

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网