I have a Spring Boot Reactive Web service application with a REST service that processes the request and redirects the response to another page like this:
@RequestMapping(value = {"/redirect/", "/{context}/redirect/"}){
public class RedirectController{
@GetMapping(value = "/{param}/**")
public Mono<ResponseEntity<Void>> doGet(ServerHttpRequest request, Locale locale, @PathVariable("param") String param) {
return applicationLogic(param) //application logic here...
.map(this::buildRedirectResponseEntity)
.contextWrite(context -> Context.of(requestContext));
}
private ResponseEntity<Void> buildRedirectResponse(String redirectUrl) {
return ResponseEntity
.status(HttpStatus.MOVED_PERMANENTLY)
.location(URI.create(redirectUrl))
.build();
}
}
When I run this Spring Boot application in my local Mac on
http://localhost:8080/redirect/test-param
and test this code in Chrome browser, the response header in Chrome is shown as expected.
HTTP/1.1 301 Moved Permanently
Location: https%3A%2F%2Fwww.test-url.com
However, the browser and the Postman Rest client loads the URL like this "http://localhost:8080/redirect/https%3A%2F%2www.test-url.com"
Why is this redirect not loading the redirect URL and attaching it as part of the original URL?
The issue here is that the redirect URL was encoded.
If the URL is not encoded, the redirect works.
Here is the HTTP response with the right URL format:
HTTP/1.1 301 Moved Permanently
Location: https://www.test-url.com
So the fix is to ensure that the redirect URL domain is not encoded.