4

Background: I'm trying to create a backend for a 'Single Page App' web client, that routes all requests to the index.html entrypoint except for anything under /assets/. "/" is mapped to /index.html in a ResourceHandler, and works fine.

Problem: I want to match all paths that do not start with '/assets/' with the RequestMapping in Spring. This works if I positively match 'assets', but not if I negatively match 'assets' in the regex.

@ControllerAdvice
@RequestMapping(value="/**")
public class SPARouter {

    @RequestMapping(value = {"/{path:(?!assets)}/**"}, method = RequestMethod.GET)
    public String router() {
        return "forward:/";
    }

}

The above code does not work as it never matches any URL. If I replace "/{path:(?!assets)}/**" with "/{path:assets}/**" it will match a URL like /assets/...

So the thing that does not work is the negative lookahead / match in the regex.

Is it possible to negatively match a pattern in this way?

Damian
  • 101
  • 1
  • 9

2 Answers2

1

I’m not too sure about the negative matching part…

But for your concrete problem (forwarding to / for everything except something), the solution I have successfully used is an interceptor. The interceptor checks on the URL, forwards if necessary and, well, doesn’t if not.

Michael Piefel
  • 18,660
  • 9
  • 81
  • 112
  • Thanks for the interceptor hint - I was originally looking for the spring boot way of interfering with the request pipeline. – Damian Oct 03 '19 at 09:29
0

Try using ^ in mapping as below

@RequestMapping(value = {"/{path:^(?!assets)}/**"}, method = RequestMethod.GET)

AbhiN
  • 642
  • 4
  • 18