首页 > 解决方案 > 如何将url作为@PathVariable传递给spring RestController?

问题描述

我想将 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);
}

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

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

标签: javaspringrestget

解决方案


用标准代码替换斜杠:%2F.

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

我已将冒号替换为%3A以防万一您对此也有疑问


推荐阅读