3

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

2
  • 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. Commented Jun 24, 2022 at 7: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. Commented Jun 24, 2022 at 8:13

1 Answer 1

1

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";
}

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.