首先,借助工具类:【Spring Boot】获取Request/Response的工具类:RequestContextHolderUtil,获取到Request对象
package com.wenjianbao.util; import javax.servlet.http.HttpServletRequest; /** * 页面URL工具类 */ public class PageURLUtil { /** * 获取当前页面URL */ public static String getPageUrl() { HttpServletRequest request = RequestContextUtil.getRequest(); return request.getScheme() + "://" + request.getServerName() + (request.getServerPort() == 80 ? "" : ":" + request.getServerPort()) + request.getContextPath() + request.getRequestURI() + (request.getQueryString().equals("") ? "" : "?" + request.getQueryString()); } }
测试:
@RequestMapping("blog") @Controller public class BlogController { @GetMapping("list") @ResponseBody public String blogList() { String pageUrl = PageURLUtil.getPageUrl(); System.out.println(pageUrl); } }
输出:
http://localhost:8001/blog/list?pageNum=2&pageSize=3