Home > Enterprise >  Reading RequestMapping wildcard in webflux application
Reading RequestMapping wildcard in webflux application

Time:01-23

I am trying to obtain the wildcard value from a controller's @requestMapping within a Spring boot webflux application:

@Controller
@RequestMapping("/template/**")
public class TemplateController {
    @GetMapping
    public Mono<String> getTemplate ( ... ) {
        String path = ...     // obtain the value of ** in the @RequestMapping
        return Mono.just(path);
    }
}

Everything I have found regarding reading such a value is for an MVC application; those solutions don't work for a webflux application.

How does one access this value within a webflux context? I would prefer an annotation-based approach for reusability, but any solution would do.

CodePudding user response:

You could get the path from ServerHttpRequest that could be obtained from ServerWebExchange passed to the controller method. From ServerHttpRequest you can access to a structured representation of the full request path

@GetMapping("/endpoint")
public Mono<Void> endpoint(ServerWebExchange serverWebExchange) {
    ServerHttpRequest request = serverWebExchange.getRequest();

    RequestPath path = request.getPath();
    String value = path.value();

    return Mono.empty();
}

With this you can play around the methods of RequestPath, maybe it helps

  • Related