Languages
[Edit]
EN

Java - redirect to external URL from controller action in Spring MVC

17 points
Created by:
Huzaifa-Ball
385

In this short article, we would like to show how to redirect from controller to external link in Spring Framework (Java Web Application).

Below you can find different approaches.

First:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;

@Controller
public class RedirectController {

    @RequestMapping(value = "/external-redirect", method = RequestMethod.GET)
    public void method(HttpServletResponse response) {

        String externalUrl = "https://some-domain.com/path/to/somewhere";
        response.setStatus(302);
        response.setHeader("Location", externalUrl);
    }
}

Second:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class RedirectController {

    @RequestMapping(value = "/external-redirect", method = RequestMethod.GET)
    public ModelAndView method() {

        String externalUrl = "https://some-domain.com/path/to/somewhere";
        return new ModelAndView("redirect:" + externalUrl);
    }
}

Third:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.view.RedirectView;

@Controller
public class RedirectController {

    @RequestMapping(value = "/external-redirect", method = RequestMethod.GET)
    public RedirectView method() {

        String externalUrl = "https://some-domain.com/path/to/somewhere";
        RedirectView redirectView = new RedirectView();
        redirectView.setUrl(externalUrl);

        return redirectView;
    }
}

Fourth:

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.net.URI;
import java.net.URISyntaxException;

@Controller
public class RedirectController {

    @RequestMapping(value = "/external-redirect", method = RequestMethod.GET)
    public ResponseEntity<Object> method() throws URISyntaxException {

        URI externalUri = new URI("https://some-domain.com/path/to/somewhere");
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setLocation(externalUri);

        return new ResponseEntity<>(httpHeaders, HttpStatus.SEE_OTHER);
    }
}
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join