title: How to redirect a Single Page Application in Spring Boot?
body: <p>I have a signup endpoint for a Spring Boot app. The UI is a React Single Page App. </p>
  
  <p>I want to make the SPA app redirect to "/login" (making a GET request). </p>
  
  <p>How should I do this? </p>
  
  <p>I tried the following two approaches:</p>
  
  <p>The first approach doesn't work as the response http code in postman is 200 and the body only includes "redirect:/login" </p>
  
  <pre><code>@RequestMapping(value = "/signup", method = RequestMethod.POST)
  public String signup(@RequestBody CustomUserDetails user, HttpServletResponse response) {
  
      String userName = user.getUsername();
      logger.debug("User signup attempt with username: " + userName);
  
      try{
          if(customUserDetailsService.exists(userName))
          {
              logger.debug("Duplicate username " + userName);
              return "redirect:/login";
          } else {
              customUserDetailsService.save(user);
              authenticateUserAndSetSession(user, response);
          }
      } catch(Exception ex) {
      }
              return "redirect:/login";
  }
  </code></pre>
  
  <p>I also tried the following approach. But it throws <code>Method Not Allowed</code> exception. Because I have a controller for <code>login</code> when the request is of type <code>POST</code></p>
  
  <pre><code>@RequestMapping(value = "/signup", method = RequestMethod.POST)
  public ModelAndView signup(@RequestBody CustomUserDetails user, ModelMap model, HttpServletResponse response) {
  
      String userName = user.getUsername();
      logger.debug("User signup attempt with username: " + userName);
  
      try{
          if(customUserDetailsService.exists(userName))
          {
              logger.debug("Duplicate username " + userName);
              return new ModelAndView("redirect:/login", model);
          } else {
              customUserDetailsService.save(user);
              authenticateUserAndSetSession(user, response);
          }
      } catch(Exception ex) {
      }
      return new ModelAndView("redirect:/redirectedUrl", model);
  }
  </code></pre>
  
  <p>How am I supposed to handle this redirect ? What is the best practice ? </p>