简体   繁体   English

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

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

I want to put URL in GET Request so I can then redirect the user to the given URL. 我想将URL放入GET Request中,以便随后可以将用户重定向到给定的URL。

This is my code so far: 到目前为止,这是我的代码:

@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);
}

Example url i use to GET - http://localhost:8080/app/controller/redirectionTest/1234/http://localhost:3000/main 我用于GET的示例网址-http:// localhost:8080 / app / controller / redirectionTest / 1234 / http:// localhost:3000 / main

So when I call the GET method I wanna run some code then be redirected to http://localhost:3000/main but the URL has slashes in it so it makes it impossible. 因此,当我调用GET方法时,我想运行一些代码,然后将其重定向到http:// localhost:3000 / main,但URL中带有斜杠,因此无法使用。

Replace the slashes with the standard code: %2F . 将斜杠替换为标准代码: %2F

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

I have replaced the colons with %3A just in case you have a problem with that one also 我已经用%3A替换了冒号,以防万一您也遇到了问题

You can try this 你可以试试这个

@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);
}

Now, visit http://localhost:8080/app/controller/redirectionTest/1234?redirectLink=http://localhost:3000/main 现在,访问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