Redirect to external URL from controller action in Spring MVC

I noticed that the following code redirects the user to a URL inside the project,

@RequestMapping(method = RequestMethod.POST) public String processForm(HttpServletRequest request, LoginForm loginForm, BindingResult result, ModelMap model) { String redirectUrl = "yahoo.com"; return "redirect:" + redirectUrl; } 

whereas the following is redirected properly as intended, but requires http: // or https: //

 @RequestMapping(method = RequestMethod.POST) public String processForm(HttpServletRequest request, LoginForm loginForm, BindingResult result, ModelMap model) { String redirectUrl = "http://www.yahoo.com"; return "redirect:" + redirectUrl; } 

I want the redirect to always be redirected to the specified URL, whether it has a valid protocol in it or not, and does not want to redirect to the view. How can i do this?

Thank,

+84
java spring spring-mvc jsp
Jul 30 '13 at 19:32
source share
9 answers

You can do this in two ways.

The first:

 @RequestMapping(value = "/redirect", method = RequestMethod.GET) public void method(HttpServletResponse httpServletResponse) { httpServletResponse.setHeader("Location", projectUrl); httpServletResponse.setStatus(302); } 

Secondly:

 @RequestMapping(value = "/redirect", method = RequestMethod.GET) public ModelAndView method() { return new ModelAndView("redirect:" + projectUrl); } 
+154
Jul 31 '13 at 3:57
source share

You can use RedirectView . Copied from JavaDoc :

A view redirecting the relative URL of an absolute, contextual relationship, or current request

Example:

 @RequestMapping("/to-be-redirected") public RedirectView localRedirect() { RedirectView redirectView = new RedirectView(); redirectView.setUrl("http://www.yahoo.com"); return redirectView; } 

You can also use ResponseEntity , for example

 @RequestMapping("/to-be-redirected") public ResponseEntity<Object> redirectToExternalUrl() throws URISyntaxException { URI yahoo = new URI("http://www.yahoo.com"); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setLocation(yahoo); return new ResponseEntity<>(httpHeaders, HttpStatus.SEE_OTHER); } 

And of course, return redirect:http://www.yahoo.com , as mentioned by others.

+48
Oct 16 '14 at 21:11
source share

Studying the actual implementation of UrlBasedViewResolver and RedirectView , the redirect will always be contextRelative if your redirect target starts with /. So sending //yahoo.com/path/to/resource would not help get the relative protocol redirection.

So, in order to achieve what you are trying, you can do something like:

 @RequestMapping(method = RequestMethod.POST) public String processForm(HttpServletRequest request, LoginForm loginForm, BindingResult result, ModelMap model) { String redirectUrl = request.getScheme() + "://www.yahoo.com"; return "redirect:" + redirectUrl; } 
+44
Aug 01 '13 at 11:57 on
source share

Another way to do this is to simply use the sendRedirect method:

 @RequestMapping( value = "/", method = RequestMethod.GET) public void redirectToTwitter(HttpServletResponse httpServletResponse) throws IOException { httpServletResponse.sendRedirect("https://twitter.com"); } 
+20
Oct 01 '15 at 21:41
source share

For an external URL, you should use " http://www.yahoo.com " as the redirect URL.

This is explained in the redirect: spring reference documentation prefix .

redirect: / myapp / some / resource

will be redirected relative to the current servlet context, while the name, for example

redirect: http://myhost.com/some/arbitrary/path

will be redirected to an absolute URL

+8
Jul 30 '13 at 20:38
source share

Works fine for me:

 @RequestMapping (value = "/{id}", method = RequestMethod.GET) public ResponseEntity<Object> redirectToExternalUrl() throws URISyntaxException { URI uri = new URI("http://www.google.com"); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setLocation(uri); return new ResponseEntity<>(httpHeaders, HttpStatus.SEE_OTHER); } 
+5
May 30 '15 at 8:19
source share

Have you tried RedirectView , where can you provide the contextRelative parameter?

+3
Jul 30 '13 at 20:54 on
source share

You can do this quite succinctly using ResponseEntity for example:

  @GetMapping ResponseEntity<Void> redirect() { return ResponseEntity.status(HttpStatus.FOUND) .location(URI.create("http://www.yahoo.com")) .build(); } 
0
Mar 28 '19 at 17:00
source share

In short, "redirect:yahoo.com" or "redirect://yahoo.com" will provide you with yahoo.com .

where "redirect:yahoo.com" will provide you with your-context/yahoo.com ie for ex localhost:8080/yahoo.com

-one
May 27 '17 at 6:20
source share



All Articles