2

Below is the request mapping method:

@GetMapping("/redirect")
    public ResponseEntity<Void> redirect() {

        String url = "http://yahoo.com";

        return ResponseEntity.status(HttpStatus.FOUND)
                .location(URI.create(url))
                .build();
    }

When I hit the URL http://somehost:8080/redirect in the browser I see that it takes me to yahoo.com, but when the /redirect is called from the UI(reactjs) the 302 Found httpstatus value is returned in the browser console but the page on the browser is blank. I was expecting to see the yahoo.com page. Seems it is not redirecting.

I referred this link: Redirect to an external URL from controller action in Spring MVC

reactjs code:

yield globalAxios.get(http://somehost:8080/redirect)

Below image when the http://somehost:8080/redirect gets called from the UI enter image description here

Below image is when we the /redirect redirects to the link: yahoo.com

enter image description here

Is it because of the 405 method not allowed error as seen in the above image

Coder17
  • 767
  • 4
  • 11
  • 30
  • 1
    As it's working as expected with a direct call, I would suggest to change/add the tags of your question to include reactjs and to add the section of the reactjs code in your question. – Fabien Jun 24 '22 at 07:24
  • 1
    You are using a JS framework to do requests and as such the browser doesn't handle the redirect the JS framework is. So you need to add code to handle the response. – M. Deinum Jun 24 '22 at 08:13

1 Answers1

0

Just in case if someone run into something like this in the future.

I end up using this code getting rid of 405 method not allowed while I am doing PUT-REDIRECT-GET pattern.

Notice it is @Controller and not @RestContorller. Otherwise it won't work.

If this is to be implemented in an existing rest controller you may want to add @ResponseBody over the other methods but not on these.

@Controller
@RequestMapping("/redirect")
public class RedirectController {

@PutMapping()
public String redirect() {
    return "redirect:/redirect";
}

@GetMapping()
public String redirectPost() {
    return "redirect:https://www.google.com";
}

}

EcOs9
  • 1
  • 2