3

How can I get the referrer URL in Spring Webflux? I tried to look into the header attributes in ServerWebExchange exchange object but could not found the same. Can someone please help me here.

Paras
  • 3,191
  • 6
  • 41
  • 77

1 Answers1

5

You just obtain it as a normal header - it doesn't really matter what mechanism you use to do this, since they all have header access.

I tried to look into the header attributes in ServerWebExchange

If you want it on ServerWebExchange, you can definitely obtain it via the following:

serverWebExchange.getRequest().getHeaders().getFirst("referer");

If you want it as a parameter to a normal REST mapping, you can just use @RequestHeader:

@GetMapping("/greeting")
public Mono<String> greeting(@RequestHeader("referer") Optional<String> referer) {
    //...
}

Or if you're using a ServerRequest:

public Mono<ServerResponse> greeting(ServerRequest request) {
    String referer = request.headers().firstHeader("referer");
    //...
}
Michael Berry
  • 70,193
  • 21
  • 157
  • 216