0

I have requirement to handle all request except few with single handler method in spring boot.

The valid urls that should be serveed -

/test/login
/test/dashboard
/test/validate/details

and the invalid urls that should not be served is -

/test/asset/login
/test/asset
/test/validate/asset

basically any URL which contains string "asset" should not be handled.

So far I am trying it as follows but it's not working -

@GetMapping("test/{path:^(?!.*(asset))}")
    String hello(String path){
        return "hello>>" + path;
    }

But this is not working. I think there is some problem with the regex that I am using.

Any help is much appreciated.

Arvind Kumar
  • 459
  • 5
  • 21

1 Answers1

2

Try this regex:

^(?:(?!asset).)*$
rotemp
  • 61
  • 7
  • Getting error - The number of capturing groups in the pattern segment (^((?!asset).)*$) does not match the number of URI template variables it defines, which can occur if capturing groups are used in a URI template regex. Use non-capturing groups instead. java.lang.IllegalArgumentException: The number of capturing groups in the pattern segment (^((?!asset).)*$) does not match the number of URI template variables it defines, which can occur if capturing groups are used in a URI template regex. Use non-capturing groups instead. – Arvind Kumar May 12 '20 at 13:41
  • Fixed to non-capturing group: ^(?:(?!asset).)*$ – rotemp May 13 '20 at 06:04