繁体   English   中英

如何将URL作为@PathVariable传递给Spring RestController?

[英]How to pass url as @PathVariable to spring RestController?

我想将URL放入GET Request中,以便随后可以将用户重定向到给定的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的示例网址-http:// localhost:8080 / app / controller / redirectionTest / 1234 / http:// localhost:3000 / main

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

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

HTTP://本地主机:8080 /应用程序/控制器/ redirectionTest / 1234 / HTTP%3A%2F%2Flocalhost%3A3000%2Fmain

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

你可以试试这个

@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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM