【问题标题】:How to pass url as @PathVariable to spring RestController?如何将url作为@PathVariable传递给spring RestController?
【发布时间】:2019-02-11 16:02:57
【问题描述】:

我想将 URL 放入 GET 请求中,这样我就可以将用户重定向到给定的 URL。

这是我目前的代码:

@RequestMapping(value = { "/mapping/{param1}/{redirectLink}" }, method = { RequestMethod.GET})
public void mapping(HttpServletResponse response, @PathVariable("param1") String param1, @PathVariable("redirectLink") String redirectLink) throws IOException {
    // code i wanna run
    response.sendRedirect(backLink);
}

我用来 GET 的示例 url - http://localhost:8080/app/controller/redirectionTest/1234/http://localhost:3000/main

因此,当我调用 GET 方法时,我想运行一些代码,然后将其重定向到 http://localhost:3000/main,但 URL 中有斜杠,因此无法实现。

【问题讨论】:

  • 为什么不使用查询参数呢?
  • 任何方式都可以,只要你用一个例子解释我如何最好地做到这一点。

标签: java spring rest get


【解决方案1】:

将斜杠替换为标准代码:%2F

http://localhost:8080/app/controller/redirectionTest/1234/http%3A%2F%2Flocalhost%3A3000%2Fmain

我已将冒号替换为 %3A,以防万一您也遇到了问题

【讨论】:

  • 您能举个例子吗?
  • 好的,明天试试
【解决方案2】:

你可以试试这个

@RequestMapping(value = { "/mapping/{param1}" }, method = { RequestMethod.GET})
public void mapping(HttpServletResponse response, @PathVariable("param1") String param1, @RequestParam(required = true, value = "redirectLink") String redirectLink) throws IOException {
    // code i wanna run
    response.sendRedirect(redirectLink);
}

现在,访问http://localhost:8080/app/controller/redirectionTest/1234?redirectLink=http://localhost:3000/main

【讨论】:

    猜你喜欢
    • 2018-11-30
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 2021-10-18
    • 2017-06-21
    相关资源
    最近更新 更多