I am building a URL Shorter app (like Bitly). It is an SPA using Spring Boot & ReactJS. All web content is served off of index.html
. All other routes are presumed to be shortLink
redirect requests which should trigger a clickShortUrl()
function to fetch the corresponding originalLink
and redirect the user to that web address.
Therefore, I want the following routes to redirect to index.html
:
@GetMapping(value = {"/", "/home", "/dashboard"})
public String redirect() {
return "forward:/index.html";
}
and all other/unknown routes to trigger a wildcard function:
@RequestMapping(value = "/{shortUrl}", method = RequestMethod.GET)
public Object clickShortUrl(@PathVariable("shortUrl") String shortUrl, @RequestBody ClickDTO request) {
// internalLogicHere
};
Individually, the mappings and functions are working. But combined, the /{shortUrl}
wildcard route always takes precedence. I've googled around looking for ways to override this behavior. It seems to be possible a few ways, but all of my attempts have failed.
I read several posts like this suggesting to extend WebMvcConfigurerAdapter
and override addViewControllers(ViewControllerRegistry registry)
to define view controllers for specific routes. I don't really understand this. Is this the right path? If so, can someone help me understand what ViewControllerRegistry
is all about and set me on the right path?
Thank you!