使用处理程序拦截器跳过调用其余控制器 class

Skip calling rest controller class using handler intercptor

我已经用 spring boot 写了一个小的休息 api。

url - http://localhost:8080/REST/api/testservice

测试控制器class

@RestController
@Scope("request")
@RequestMapping("/api")
public class TestRestController {
    private final Log logger = LogFactory.getLog(getClass());

    @Autowired
    Common common;

    @Autowired
    CommonVarList commonVarList;


    @CrossOrigin
    @RequestMapping(value = "/testservice", method = RequestMethod.GET)
    public ResponseEntity<ResponseBean> getCheckUser() {
        return new ResponseEntity<ResponseBean>(new ResponseBean(Boolean.parseBoolean(commonVarList.BOOLEAN_DEFAULT_TRUE),common.getLocaleMessage(MessageVarList.IB_SERVICE_RUNNING)), HttpStatus.OK);
    }
}

我也写了拦截器class

拦截器class

public class TestRestInterceptor implements HandlerInterceptor {
    final static Logger logger = Logger.getLogger(IbRestInterceptor.class);

    @Autowired
    Common common;

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        //check request time out here
        boolean isRequestTimeOut = common.checkUserTimeOut();
        if(isRequestTimeOut){
            httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/api/request/timeout");
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        //TODO
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {}
}

超时休息控制器class

@RestController
@Scope("request")
@RequestMapping(path = "/api/request")
public class ResponseRestController {

    @Autowired
    RequestHandlerService requestHandlerService;

    @CrossOrigin
    @RequestMapping(value = "/timeout", method = RequestMethod.GET)
    public ResponseEntity<ResponseBean> postUserTimeOut(){
        ResponseBean responseBean=requestHandlerService.handleRequestTimeOut();
        return new ResponseEntity<ResponseBean>(responseBean,HttpStatus.REQUEST_TIMEOUT);
    }
}

我已经使用 httpServletResponse.sendRedirect 跳过调用 /api/testservice 休息控制器 class 当用户请求超时。

但它会调用 /api/testservice 休息控制器 class 然后调用 /api/request/timeout休息控制器 class.

我想在用户请求超时时跳过调用 /api/testservice 休息控制器 class。

是否有人可以描述如何解决此问题。这会很有帮助。提前致谢

文档说

DispatcherServlet processes a handler in an execution chain, consisting of any number of interceptors, with the handler itself at the end. With this method, each interceptor can decide to abort the execution chain, typically sending a HTTP error or writing a custom response.

和描述 preHandle 方法

Returns: true if the execution chain should proceed with the next interceptor or the handler itself. Else, DispatcherServlet assumes that this interceptor has already dealt with the response itself.

因此,如果您抛出异常或在拦截器中写入响应并且 return 为 false,调用将不会到达实际的处理程序(控制器)。