I am hitting a controller in spring boot application and from there when i return to browser i want it to redirect to another website or url other than spring boot application templates.
Asked
Active
Viewed 5,318 times
2
-
1Please check this question: https://stackoverflow.com/a/17961336/865448 – tostao Sep 11 '19 at 19:47
1 Answers
4
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);
}

MyTwoCents
- 7,284
- 3
- 24
- 52