Search code examples
javaspring-webfluxspring-reactivespring-boot-2spring5

How can I get the referrer URL in Spring Webflux?


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.


Solution

  • 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");
        //...
    }