【问题标题】:How to read the url from request如何从请求中读取 url
【发布时间】:2012-03-12 00:16:39
【问题描述】:

我想使用 spring 读取请求 url,我有如下方法,客户端请求 url 类似于 http://localhost:8080/api/getName,我想读取(来自这个 url 的 /api/getName)

@Controller
public class TestController  {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ResponseEntity<String> getDetails(
            final HttpServletRequest request,
            final HttpServletResponse response) throws Exception {

        }

【问题讨论】:

  • 为什么?您是否要将自己的调度机制放在 Spring 中已有的调度机制之上?

标签: java spring


【解决方案1】:

这是HttpServletRequest 中的一个方法:request.getRequestURL() 为您提供 URL。

有关更多详细信息,请参阅此答案:How to get the request url from HttpServletRequest

要进一步分析 URL,请使用它的方法:Parsing a URL

【讨论】:

【解决方案2】:

我想阅读(/api/getName 来自这个 url)

为此,您可以使用 HttpServletRequest 方法getRequestURI()。它将返回请求 URL,没有协议/服务器/端口部分,也没有查询字符串(参数)。

【讨论】:

    【解决方案3】:

    【讨论】:

      【解决方案4】:

      req.getContextPath();

      应该为您提供您正在寻找的上下文路径。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-07
        相关资源
        最近更新 更多