6

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?

3
  • 4
    stackoverflow.com/questions/52003857/… Check that Commented Oct 3, 2019 at 9:17
  • That nailed it - thanks Commented Oct 3, 2019 at 9:25
  • So what was the working regex?? Commented Sep 4, 2024 at 7:15

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the interceptor hint - I was originally looking for the spring boot way of interfering with the request pipeline.
0

Try using ^ in mapping as below

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

1 Comment

Unfortunately it doesn't work. One of the many variations of the regex I've tried.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.