http请求转发、springboot请求转发、servlet转发请求

http请求转发、springboot请求转发、servlet转发请求

将请求进行应用内转发:

/api/user → /user
/api/info → /info

应用场景:项目升级兼容时

    @RequestMapping("/api/**")
    public void apiProxy(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String uri = request.getRequestURI();
        uri = uri.substring(4);// 将 /api截取保留后面的/**
        log.info("请求转发: {} - {}", request.getMethod(), uri);
        // 创建RequestDispatcher对象,用于转发请求
        RequestDispatcher dispatcher = request.getRequestDispatcher(uri);
        // 调用forward方法转发请求
        dispatcher.forward(request, response);
    }

你可能感兴趣的:(java,http,spring,boot,servlet)