Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 如何使用拦截器获取控制器执行时间并将其注入bean中以json格式显示_Spring_Spring Mvc - Fatal编程技术网

Spring 如何使用拦截器获取控制器执行时间并将其注入bean中以json格式显示

Spring 如何使用拦截器获取控制器执行时间并将其注入bean中以json格式显示,spring,spring-mvc,Spring,Spring Mvc,我正在使用Spring,我对拦截器非常陌生。现在,我正在控制器中使用System.currentTimeMillis()捕获每个请求的执行时间,在bean中设置执行时间,并返回该bean以JSON格式显示 现在我想使用拦截器来帮助我的应用程序实现这些功能。我测试了一些示例,但是在从控制器返回bean值后调用了postHandle(..)和afterCompletion(),我无法设置executionTime 下面是我的示例bean属性: private String status; priva

我正在使用Spring,我对拦截器非常陌生。现在,我正在控制器中使用
System.currentTimeMillis()
捕获每个请求的执行时间,在bean中设置执行时间,并返回该bean以JSON格式显示

现在我想使用拦截器来帮助我的应用程序实现这些功能。我测试了一些示例,但是在从控制器返回bean值后调用了
postHandle(..)
afterCompletion()
,我无法设置
executionTime

下面是我的示例bean属性:

private String status;
private Long executionTime;
private String message;
private String id;  

请您为我提供正确的建议。谢谢

在当前需求中,计算方法执行时间是横切关注点

因此,使用Spring AOP将非常有用, 使用AOP API中的建议

Around建议是Spring AOP中最强大的建议。原因是,此建议可以在方法调用前后执行自定义行为

我把下面的链接给你看


如何设置值?向我们展示codelong exeTime=endTime StartTime;sampleBeanObj.setExecutionTime(exeTime);这些代码在我的控制器中。现在,我想让拦截器计算请求执行时间,并以某种方式在bean中为我提供时间值,因为我正在将bean作为json格式呈现在显示屏上。谢谢。我的意思是,您将此代码放在拦截器中的何处?请检查下面的拦截器代码,并尝试了解我的基本要求。我想使用拦截器获取控制器请求执行时间,然后我需要将该值与名称qTime绑定,其他人正在使用我的RESTAPI。我只需要使用一个参数qTime={}公开RESTAPI,该参数将向rest客户机用户显示所用的时间。现在请帮助我如何使用拦截器。我的响应格式是Json。非常感谢Pramod,共享链接很有用,但它与向方法添加方面有关。正如我们所知,拦截器可以在控制器请求上钩住事件,它为添加所有方法名节省了我们的时间。我们可以使用AOP这样的方法来调用每个控制器请求吗?请在适当的时候给予信任:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class ExecuteTimeInterceptor extends HandlerInterceptorAdapter{

private static final Logger logger =  Logger.getLogger(ExecuteTimeInterceptor.class);

//before the actual handler will be executed
 @Override
public boolean preHandle(HttpServletRequest request, 
    HttpServletResponse response, Object handler)
    throws Exception {

    long startTime = System.currentTimeMillis();

    System.out.println("startTime==="+startTime); 

    request.setAttribute("startTime", startTime);

    return true;
}

//after the handler is executed
 @Override
public void postHandle(
    HttpServletRequest request, HttpServletResponse response, 
    Object handler, ModelAndView modelAndView)
    throws Exception {

    long startTime = (Long)request.getAttribute("startTime");

    long endTime = System.currentTimeMillis();

    long executeTime = endTime - startTime;

    System.out.println("postHandle executeTime==="+executeTime);

    //modified the exisitng modelAndView
    //modelAndView.addObject("executeTime",executeTime);
    //user3563589

    //log it
    if(logger.isDebugEnabled()){
       logger.debug("[" + handler + "] executeTime : " + executeTime + "ms");
    }
}

 @Override
    public void afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
     long startTime = (Long)request.getAttribute("startTime");

        long endTime = System.currentTimeMillis();

        long executeTime = endTime - startTime;

        System.out.println("afterCompletion executeTime==="+executeTime);

        request.setAttribute("executeTime", String.valueOf(executeTime));

        System.out.println("After completion handle");
    }

}