不灭的焱

革命尚未成功,同志仍须努力下载JDK17

作者:php-note.com  发布于:2021-05-25 17:10  分类:Java框架/系统  编辑

首先,借助工具类:【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

 

附:Hutool】URL生成器-UrlBuilder