Spring boot 获取全局的Request 和Response

Spring boot
获取全局的Request 和Response


ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
HttpServletResponse response = requestAttributes.getResponse();

通过Resquest获得URL

 
    /**
     * 获取HttpServletRequest
     * @return
     */
    public static HttpServletRequest getHttpServletRequest(){
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    /**
     * 获取HttpServletResponse
     * @return
     */
    public static HttpServletResponse getHttpServletResponse(){
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse() ;
    }
    
	 /**
     * 获取请求路径
     * @return
     */
    public static String getUrl()
    {
        return getHttpServletRequest().getRequestURI();
    }

    /**
     * 获取头信息
     * @param jwt
     * @return
     */
    public static String getHeader(String jwt)
    {
        return getHttpServletRequest().getHeader(jwt);
    }

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