1

I want my application to route to "/" whenever browser go to any path except "/api".

here's what i did on my redirectIfNotApi() method in my controller.

@Controller
@RequestMapping("/")
class MyController {

    @RequestMapping(value = "{^(?!api).$}")
    public String redirectIfNotApi() {
        System.out.println("Route to VueJS anything except /api");
        return "forward:/";
    }
}

regex {^(?!api).$} does not work. Everytime i try to go to localhost:8080/, redirectIfNotApi() does not called.

please help. Thank you.

tuty_fruity
  • 523
  • 1
  • 5
  • 18

2 Answers2

0

As per the spring documentation link below : https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates-regex

We need to follow the syntax that is {varName:regex}

Please check if this works.

Mat be {varname:^(?!api).$} might work !!

0

The issue is with the regex that you are using. The right regex to match absence of api is ^((?!api).)*$. So replace the regex as below:

@RequestMapping(value = "{^((?!api).)*$}")
public String redirectIfNotApi() {
    System.out.println("Route to VueJS anything except /api");
    return "forward:/";
}
Madhu Bhat
  • 13,559
  • 2
  • 38
  • 54