How to redirect to an external URL from Spring Boot REST Controller (Post/Redirect/Get pattern)?

Let’s say you want to redirect users to an external URL when they make an API request.

How to do this in Spring Boot?

There are two ways you can do this.

One using ResponseEntity object

Two using RedirectView object.

Here is how to do it with ResponseEntity object:

STEP1: Create a REST Controller which returns Void Response Entity

STEP2: Build a response entity with FOUND HttpStatus (302 code) and send the URL along with it

Here is the implementation in detail:

STEP1: Create a REST Controller which returns Void ResponseEntity

Here is a sample:

   @PostMapping(value = "/redirect")
    public ResponseEntity<Void> redirect(@RequestParam Map<String,String> input){

    
    }

You can get the input using @RequestParam annotation and do any processing you want with the input before doing the redirection

The return type of the method should be ResponseEntity<Void>.

STEP2: Build response entity object with 302 http status code and the external URL.

Here is a sample:

   @PostMapping(value = "/redirect")
    public ResponseEntity<Void> redirect(@RequestParam Map<String,String> input){

        System.out.println(input);

        return ResponseEntity.status(HttpStatus.FOUND).location(URI.create("https://fullstackdeveloper.guru")).build();
    }

As you can see , we are building a response entity object with HttpStatus FOUND (302) and then passing this blog’s address .

Here is the entire REST Controller class:

package com.example.redirect;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.print.DocFlavor;
import java.net.URI;
import java.util.Map;

@RestController
public class RedirectController {

    @PostMapping(value = "/redirect")
    public ResponseEntity<Void> redirect(@RequestParam Map<String,String> input){

        System.out.println(input);

        return ResponseEntity.status(HttpStatus.FOUND).location(URI.create("https://fullstackdeveloper.guru")).build();
    }
}

I deployed this in localhost:8080.

Now let’s test it from UI code.

I created a simple HTML page form like this:

<html>


<body>


    <form action="http://localhost:8080/redirect" method="POST">

<input id="msg" name="message"></input>

        <button type="submit">Submit</button>


    </form>
</body>



</html>

I have just created a html form which takes a single input and submits it to the rest service I had created and deployed to port 8080 in my local.

I just entered the message hello and clicked on submit.

I got redirected to my blog:

Also the message hello got printed in the console:

Now let’s see how to do it using Redirect View:

STEP1: Create a REST Controller which returns a RedirectView

STEP2: Create a RedirectView object , set the external URL and return it.

Here is the updated code using RedirectView:

 @PostMapping(value = "/redirect")
    public RedirectView redirect(@RequestParam Map<String,String> input){

        System.out.println(input);


        RedirectView redirectView = new RedirectView();
        redirectView.setUrl("https://fullstackdeveloper.guru");

        return redirectView;
      
    }

This is also called as PRG design pattern (POST / REDIRECT / GET)

Why is this called so ?

Because ,

You first make a POST request from your HTML form

Your service then builds a redirect URL

Your html page then makes a GET request to the URL passed to it.

That’s it!

Comments

3 responses to “How to redirect to an external URL from Spring Boot REST Controller (Post/Redirect/Get pattern)?”

  1. Lucian Avatar
    Lucian

    Very clear and simple, thank you, you saved my day 🙂

    1. Vijay SRJ Avatar
      Vijay SRJ

      Thank you very much for leaving this feedback ☺️

  2.  Avatar
    Anonymous

    Hi Vijay, thank you for such a nice explanation, very helpful.
    Can you please show how to add headers while redirecting using RedirectView

Leave a Reply

Discover more from The Full Stack Developer

Subscribe now to keep reading and get access to the full archive.

Continue reading