Arvind Kasale
Arvind Kasale

Reputation: 31

Redirection to an external absolute URL in spring-boot-starter-webflux without a View Resolver

I am creating an application using spring-webflux. I have exposed a controller which has a method that is reactive and returns a Mono as the return type. The stack is reactive. The request comes in the browser and the API is expected to redirect the user in the browser to a url based on some calculation which are async in nature. Instead of redirection right now I am getting a response as shown below.

Is there a way to achieve this?

{
"defaultCharset": "UTF-8",
"requestContextAttribute": null,
"beanName": null,
"applicationContext": null,
"url": "https://my.box.com/service/auth/oauth/authorize?client_id=3d58a6ae9da221193d688feb521cf3f78ab8f1e04b3110813798b6feb877aa4d&response_type=code&redirect_uri=https://memini.serveo.net/api/v1/sources/BOX/connection?state=79299a49-e716-4ac5-a4f1-d16a2fdf6de5",
"contextRelative": true,
"statusCode": "SEE_OTHER",
"propagateQuery": false,
"hosts": null,
"redirectView": true,
"supportedMediaTypes": [
{
"type": "text",
"subtype": "html",
"parameters": {
"charset": "UTF-8"
},
"qualityValue": 1,
"wildcardType": false,
"wildcardSubtype": false,
"charset": "UTF-8",
"concrete": true
}
]
}

Upvotes: 1

Views: 2256

Answers (2)

Dupinder Singh
Dupinder Singh

Reputation: 7779

This helped me:

@Configuration public class RedirectHandler {

@Bean
public RouterFunction<ServerResponse> route() {
    return RouterFunctions.route()
        .GET("/redirect", this::redirect) // Define your redirect route
        .build();
}

public Mono<ServerResponse> redirect(ServerRequest request) {
    // Perform any necessary logic here, if needed
    String targetUrl = "https://example.com"; // Define the target URL you want to redirect to
    return ServerResponse.temporaryRedirect(URI.create(targetUrl)).build();
}

}

Upvotes: 1

Arvind Kasale
Arvind Kasale

Reputation: 31

Well made it work using the code below

@GetMapping("/")
Mono<Void> redirect(ServerHttpResponse response) {
  response.setStatus(TEMPORARY_REDIRECT);
  response.getHeaders().setLocation(URI.create(authorizationUrl));
  return response.setComplete();
}

Upvotes: 1

Related Questions