Redirect to an External Url from Controller Action in Spring MVC

Redirect to an external URL from controller action in Spring MVC

You can do it with two ways.

First:

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

Second:

@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public ModelAndView method() {
return new ModelAndView("redirect:" + projectUrl);
}

Redirect to an external URL in Spring MVC

The protocol is required if the host is different to that of the current host

String redirectUrl = "http://www.yahoo.com";
return "redirect:" + redirectUrl;

Have a look at the redirect: prefix section from Spring Web MVC framework

A logical view name such as redirect:/myapp/some/resource will redirect relative to the current Servlet context, while a name such as redirect:http://myhost.com/some/arbitrary/path will redirect to an absolute URL.

Url redirection from a spring boot controller

You can do it in many ways

Using RedirectView

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

Using ResponseEntity

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

Using HttpServletResponse

@RequestMapping(value = "/", method = RequestMethod.GET)
public void redirectToTwitter(HttpServletResponse httpServletResponse) throws IOException {
httpServletResponse.sendRedirect("https://twitter.com");
}

Using ModelAndView

@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public ModelAndView method() {
return new ModelAndView("redirect:" + projectUrl);
}

Redirect to External URl in Spring MVC

Prefix your url string with the protocol, i.e. http://www.google.com.

final String redirectUrl = "redirect:http://www.google.com";
return redirectUrl;

Redirect to External URl in Spring MVC by RequestMethod.POST

RedirectView class has created GET action and it can't POST request.

I put refId in ModelMap object and redirect to jsp.

   map.put("refId", refId);

So when page loaded then create post request form submit.

  $(document).ready(function () {
if (${not empty refId}) {
var refId = ('${refId}');
document.getElementById('bank-card-form').action = 'https://bpm.shaparak.ir/pgwchannel/startpay.mellat?RefId=' + refId;
document.getElementById('bank-card-form').submit();

}
});

And in my jsp added bank-card-form

 <form method="post" id="bank-card-form"
style="display: none" autocomplete="off">
</form>

Spring, redirect to external url using POST

Like @stepanian said, you can't redirect with POST.
But there are few workarounds:

  1. Do a simple HttpUrlConnection and use POST. After output the response stream. It works, but I had some problem with CSS.
  2. Do stuff in your controller and after redirect the result data to a fake page. This page will do automatically the POST through javascript with no user interaction (more details):

html:

<form name="myRedirectForm" action="https://processthis.com/process" method="post">
<input name="name" type="hidden" value="xyz" />
<input name="phone" type="hidden" value="9898989898" />
<noscript>
<input type="submit" value="Click here to continue" />
</noscript>
</form>
<script type="text/javascript">

$(document).ready(function() {
document.myRedirectForm.submit();
});

</script>


Related Topics



Leave a reply



Submit